[英]Error while doing HTTP Request
I am using Typhoeus gem to execute HTTP requests in my rails Application. 我正在使用Typhoeus gem在我的rails应用程序中执行HTTP请求。 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. 在执行此操作时,auth_token已转换为auth%5Ftoken = abcd。 Actually my API expecting the parameter auth_token.
实际上,我的API需要参数auth_token。 Because of this API is not allowing to access it.
由于该API不允许访问它。 It's throwing unauthorized error.
引发未经授权的错误。 Please help me to solve this problem.
请帮我解决这个问题。 Thanks in Advance.
提前致谢。
I corrected that issue with to_query. 我使用to_query更正了该问题。 Actually to_query is parsing the actual data to the API
实际上to_query正在将实际数据解析到API
Here is an example using RestClient that shows a correct submission of the data, I expect Typhoeus not to be different: 这是一个使用RestClient的示例,该示例显示了正确的数据提交,我希望Typhoeus不会有所不同:
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\"}}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.