How should I send this JSON in Faraday using the post method with the "application/x-www-form-urlencoded" and "multipart/form-data;" headers?
message = {
"name":"John",
"age":30,
"cars": {
"car1":"Ford",
"car2":"BMW",
"car3":"Fiat"
}
}
I've tried:
conn = Faraday.new(url: "http://localhost:8081") do |f|
f.request :multipart
f.request :url_encoded
f.adapter :net_http
end
conn.post("/", message)
This cURL request works
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
But I don't quite know how to get this working in Faraday. Also the data in the cURL request isn't nested JSON, so I need to be able to dynamically create the body of the request as I won't know ahead of time the exact structure of the JSON.
And please ask any questions if you need more details or clarity.
Thanks!
The default content type for POST is
x-www-form-urlencodedso a hash will be automatically encoded. There isn't such automatic data handling for JSON which is why the second example below passes in a stringified representation of the hash.I'm not sure what you're intending to do but you could send something like the following
However, this would be interpreted to be
{"{\"foo\":1,\"bar\":2}" => nil}in Ruby. If you're parsing the data at the other end you can make it work but it's always harder to fight convention.