Error while doing HTTP Request
-
21-12-2019 - |
Question
I am using Typhoeus gem to execute HTTP requests in my rails Application. The request is as follows.
data = {"auth_token"=>"abcd" ,"employee" => {"method" => "add_employee"}}
header = { "Content-Type" => "application/json","Accept"=>"application/json"}
request = Typhoeus::Request.post("www.example.com",:body=> data.to_json,:headers => header)
here while executing this the auth_token has been converted to auth%5Ftoken=abcd. Actually my API expecting the parameter auth_token. Because of this API is not allowing to access it. It's throwing unauthorized error. Please help me to solve this problem. Thanks in Advance.
Solution
I corrected that issue with to_query. Actually to_query is parsing the actual data to the API
OTHER TIPS
Here is an example using RestClient that shows a correct submission of the data, I expect Typhoeus not to be different:
data = {"auth_token"=>"abcd" ,"employee" => {"method" => "add_employee"}}
header = { "Content-Type" => "application/json","Accept"=>"application/json"}
RestClient.post("www.example.com", data.to_json, header){ |response, request, result|
puts "PAYLOAD:"+request.args[:payload]
}
Here is the payload, as expected:
PAYLOAD: "{\"auth_token\":\"abcd\",\"employee\":{\"method\":\"add_employee\"}}"
Using Typhoeus:
data = {"auth_token"=>"abcd" ,"employee" => {"method" => "add_employee"}}
header = { "Content-Type" => "application/json","Accept"=>"application/json"}
request = Typhoeus::Request.post("www.example.com",:body=> data.to_json,:headers => header)
request.request.original_options[:body]
As expected, this is the payload of the request. So your code is good!
"{\"auth_token\":\"abcd\",\"employee\":{\"method\":\"add_employee\"}}"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow