我使用Typhoeus-gem在rails应用程序中执行HTTP请求。请求如下。
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)
在执行此操作时,auth_token已转换为auth%5Ftoken=abcd。实际上我的API期望参数auth_token。因为这个API不允许访问它。它抛出未经授权的错误。请帮我解决这个问题。提前感谢。
我用to_query更正了这个问题。实际上to_query是将实际数据解析到API
这里有一个使用RestClient的例子,它显示了数据的正确提交,我希望伤寒不会有什么不同:
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]
}
这是预期的有效载荷:
PAYLOAD: "{"auth_token":"abcd","employee":{"method":"add_employee"}}"
使用伤寒:
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]
正如预期的那样,这是请求的有效负载。所以你的代码很好!
"{"auth_token":"abcd","employee":{"method":"add_employee"}}"