Rails RestClient POST 请求因“400 错误请求"而失败; [英] Rails RestClient POST request failing with "400 Bad Request"
本文介绍了Rails RestClient POST 请求因“400 错误请求"而失败;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
查看文档,没有任何关于如何发出 POST 请求的好例子.我需要使用 auth_token
参数发出 POST 请求并获得响应:
Looking at the docs there aren't any good examples of how to make a POST request. I need to make a POST request with a auth_token
parameter and get a response back:
response = RestClient::Request.execute(method: :post,
url: 'http://api.example.com/starthere',
payload: '{"auth_token" : "my_token"}',
headers: {"Content-Type" => "text/plain"}
)
400 错误请求错误:
400 bad request error:
RestClient::BadRequest: 400 Bad Request
from /Users/me/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rest-client-1.8.0/lib/restclient/abstract_response.rb:74:in `return!'
from /Users/me/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rest-client-1.8.0/lib/restclient/request.rb:495:in `process_result'
from /Users/me/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rest-client-1.8.0/lib/me/request.rb:421:in `block in transmit'
有什么好的例子说明如何使用 RestClient 发出 POST 请求?
Any good examples how to make a POST request using RestClient?
这是我在模型中提出请求的方式:
This is how I make the request in the model:
def start
response = RestClient::Request.execute(method: :post,
url: 'http://api.example.com/starthere',
payload: '{"auth_token" : "my_token"}',
headers: {"Content-Type" => "text/plain"}
)
puts response
end
推荐答案
尝试使用这样的哈希:
def start
url= 'http://api.example.com/starthere'
params = {auth_token: 'my_token'}.to_json
response = RestClient.post url, params
puts response
end
这篇关于Rails RestClient POST 请求因“400 错误请求"而失败;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文