Rails RestClient POST请求失败并显示"400 Bad Request"

Tom*_*Tom 5 ruby-on-rails rest-client

查看文档,没有任何关于如何发出POST请求的好例子.我需要使用auth_token参数发出POST请求并获得响应:

response = RestClient::Request.execute(method: :post,
                        url: 'http://api.example.com/starthere',
                        payload: '{"auth_token" : "my_token"}',
                        headers: {"Content-Type" => "text/plain"}
                       )
Run Code Online (Sandbox Code Playgroud)

400错误的请求错误:

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'
Run Code Online (Sandbox Code Playgroud)

如何使用RestClient发出POST请求的任何好例子?

编辑:

这是我在模型中提出请求的方式:

  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
Run Code Online (Sandbox Code Playgroud)

Paf*_*fjo 0

如果您只想复制curl请求:

response = RestClient::Request.execute(method: :post, url: 'http://api.example.com/starthere',  payload: {"auth_token" => "my_token"})
Run Code Online (Sandbox Code Playgroud)

当以此格式发布数据时,Curl 和 RestClient 默认为相同的内容类型 (application/x-www-form-urlencoded)。