[英]Issue with sending a POST request from Rails to Local Webservice
我正在尝试向 Web 服务发送 POST 请求。 我使用的 curl 命令是:
curl --silent --header 'Accept: application/json' --header 'Content-Type: application/json' --data
'{"mwtype":"serviceproviders/GetReport","url":"<an url>"}' http://localhost:8080/service/serviceproviders/v1
并且能够成功给出响应。 在我的铁路代码中,我这样做:
uri = URI.parse("http://localhost:8080/service/serviceproviders/v1")
res = Net::HTTP.post_form(uri, 'mwtype' => 'serviceproviders/GetReport', 'url' => '<some url>')
if res.is_a?(Net::HTTPSuccess)
puts res.body
else
puts "Something went wrong"
puts res
end
但我收到了#<Net::HTTPBadRequest 400 Bad Request readbody=true>
。 我怎样才能解决这个问题?
在您的 cURL 请求中,您发送的是 JSON。但Net::HTTP.post_form
用于发送application/x-www-form-urlencoded
表单数据对。
相反,您想使用普通post
方法:
data = { 'mwtype' => 'serviceproviders/GetReport', 'url' => '<some url>' }
uri = URI.parse("http://localhost:8080/service/serviceproviders/v1")
res = Net::HTTP.post(uri, data.to_json, "Content-Type" => "application/json")
if res.is_a?(Net::HTTPSuccess)
puts res.body
else
puts "Something went wrong"
puts res
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.