I am creating a PUT request, which requires a JSON object Array to be sent in the payload. I have put the data into an array, and have confirmed it's Array class, but when it is supplied as an argument to Json.dump() it is outputted as a String Object, which causes a 500 Internal Server Error, is there an alternative method of pushing the data, which will keep it in its original format?
def call_api
url = URI("url.zendesk.com/api/v2/macros/update_many")
https = Net::HTTP.new(url.host, url.port)
https.use_ssl = true
request = Net::HTTP::Put.new(url)
request["Authorization"] = "Basic ="
request["Content-Type"] = "application/json"
request.body = JSON.dump(@data) #outputs a string, should be a json array
response = https.request(request)
end
Reponse => #<Net::HTTPInternalServerError 500 Internal Server Error readbody=true>
CodePudding user response:
...but when it is supplied as an argument to Json.dump() it is outputted as a String Object
By definition, JSON is a string. It takes data and turns it into a string which can then be turned back into data. This is serialization. So that part is fine.
You probably need to add a scheme to your URL like https
. Newer versions of Net::HTTP won't even accept a URI without a scheme. That might be the source of the 500 error.
Looking at the API documentation, the proper URL ends in .json
.
url = URI("https://url.zendesk.com/api/v2/macros/update_many.json")
It does not take an array, it takes an object like so. Getting this format wrong should result in a 400 Bad Request.
@data = {
"macros": [
{
"active": false,
"id": 25
},
{
"id": 23,
"position": 5
}
]
}
Finally, there exists an official Zendesk Ruby API client which might be easier to use.