简体   繁体   English

将 POST 请求从 Rails 发送到本地 Web 服务时出现问题

[英]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.

相关问题 发送POST请求轨道 - Sending POST request rails 在Rails中发送POST请求 - Sending POST request in Rails Rails 4:将.txt文件从本地计算机发送到Windows共享时出现问题 - Rails 4: Issue Sending .txt files from local machine to Windows Share 通过发布请求将图像从Ionic应用发送到Rails端点 - Sending an image via a post request from an Ionic app to a rails endpoint 从 API 接收 POST 请求时出现 Rails CSRF 问题 - Rails CSRF issue on receiving POST request from API Rails JQuery发送GET而不是POST请求 - Rails JQuery sending GET instead of POST request 跨域请求被阻止:将 POST 请求从 Angular2 应用程序发送到 Rails API - Cross-Origin Request Blocked: While Sending the POST request from Angular2 Application to Rails API 使用rest_client从rails发送发布请求。400错误的请求 - Sending post request from rails with rest_client.. 400 Bad request Rails 3.2-从客户端应用程序发送POST请求会带来任何安全风险吗? - Rails 3.2 - Would sending a POST request from a client application pose any security risks? 从Flex应用程序向Ruby on Rails后端发送发布请求时出现问题 - Problem sending post request from Flex app to Ruby on Rails back end
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM