I want to send an array of JSON objects in a put request body.
JSON body:
[
{
"a":1,
"b":2
},
{
"a":2,
"b":1
}
]
I am using HTTP::put to make the request. I send data in the Json as a hash.
put("url", data: json)
But, this causes problems as on the client side it is decoded as :
[
"data":[{
"a":1,
"b":2
},
{
"a":2,
"b":1
}]
]
How do I send the Json without hashing or is there any way to get the values out of the hash before sending??
Try to define Content-Type
:
json_data = '[{"a":1,"b":2},{"a":2,"b":1 }]'
hash_body = JSON.parse json_data
uri = URI.parse('https://www.google.com/api/something')
request = Net::HTTP::Put.new(uri.request_uri, 'Content-Type' => 'application/json')
request.body = hash_body
response = http.request(request)
Cheers!
We couldn't use HTTP::Put.new because of some parameters that were set internally. We solved it by adding a method in the Her library that in turn uses Faraday to make the request. I am posting the method used here.
response = self.use_api.connection.put do |req|
req.url url
req.headers['Content-Type'] = 'application/json'
req.body = json
end
I am not sure if this is the best method or not.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.