如何使用 Faraday 的 post 方法将 JSON 作为表单数据发送

wul*_*ong 5 json multipartform-data url-encoding faraday

我应该如何使用带有“application/x-www-form-urlencoded”和“multipart/form-data;”的 post 方法在 Faraday 中发送此 JSON 标题?

message = {
  "name":"John",
  "age":30,
  "cars": {
    "car1":"Ford",
    "car2":"BMW",
    "car3":"Fiat"
  }
 }
Run Code Online (Sandbox Code Playgroud)

我试过了:

conn = Faraday.new(url: "http://localhost:8081") do |f|
  f.request :multipart
  f.request :url_encoded
  f.adapter :net_http
end

conn.post("/", message)
Run Code Online (Sandbox Code Playgroud)

这个 cURL 请求有效

curl -X POST \
  http://localhost:8081 \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
  -F 'message=2018-12-27 12:52' \
  -F source=RDW \
  -F object_type=Responses
Run Code Online (Sandbox Code Playgroud)

但我不太知道如何让它在法拉第工作。此外,cURL 请求中的数据不是嵌套的 JSON,因此我需要能够动态创建请求正文,因为我不会提前知道 JSON 的确切结构。

如果您需要更多详细信息或清晰度,请提出任何问题。

谢谢!

Aar*_*ron 6

POST 的默认内容类型是x-www-form-urlencoded自动编码哈希值。JSON 没有这样的自动数据处理,这就是为什么下面的第二个示例传递哈希的字符串化表示形式。

Faraday.new(url: 'http://localhost:8081').post('/endpoint', {foo: 1, bar: 2})
# => POST http://localhost:8081/endpoint
#         with body 'bar=2&foo=1'
#         including header 'Content-Type'=>'application/x-www-form-urlencoded'

Faraday.new(url: 'http://localhost:8081').post('/endpoint', {foo: 1, bar: 2}.to_json, {'Content-Type'=>'application/json'})
# => POST http://localhost:8081/endpoint
#         with body '{"foo":1,"bar":2}'
#         including header 'Content-Type'=>'application/json'
Run Code Online (Sandbox Code Playgroud)

我不确定您打算做什么,但您可以发送如下内容

Faraday.new(url: 'http://localhost:8081').post('/endpoint', {foo: 1, bar: 2}.to_json)
# => POST http://localhost:8081/endpoint
#         with body '{"foo":1,"bar":2}'
#         including header 'Content-Type'=>'application/x-www-form-urlencoded'
Run Code Online (Sandbox Code Playgroud)

但是,这将被解释为{"{\"foo\":1,\"bar\":2}" => nil}Ruby 中的内容。如果你在另一端解析数据,你可以让它工作,但打破惯例总是更难。