[英]Issue with sending a POST request from Rails to Local Webservice
I am trying to send a POST request to a Web Service.我正在尝试向 Web 服务发送 POST 请求。 The curl command I am using is:
我使用的 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
and it was able to successfully give a response.并且能够成功给出响应。 In my Rail Code, I am doing this:
在我的铁路代码中,我这样做:
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
but I am getting a #<Net::HTTPBadRequest 400 Bad Request readbody=true>
.但我收到了
#<Net::HTTPBadRequest 400 Bad Request readbody=true>
。 How can I fix this?我怎样才能解决这个问题?
In your cURL request you're sending JSON. But Net::HTTP.post_form
is used to send form data pairs for application/x-www-form-urlencoded
.在您的 cURL 请求中,您发送的是 JSON。但
Net::HTTP.post_form
用于发送application/x-www-form-urlencoded
表单数据对。
Instead you want to use the normal post
method:相反,您想使用普通
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.