Rails RestClient POST request failing with "400 Bad Request"
Asked Answered
J

3

6

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 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'

Any good examples how to make a POST request using RestClient?

EDIT:

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
Judenberg answered 3/2, 2016 at 11:52 Comment(7)
Are you sure that your request is text/plain request?Limitless
@Limitless curl works so I guess it is right? 'curl "api.example.com/starthere" -d "auth_token=my_token"'Judenberg
How are you handling the request at the controller? Can you post the related code?Limitless
@Limitless I'm simply printing it to console to see the response as of nowJudenberg
Try changing {"Content-Type" => "text/plain"} to {"Content-Type" => "text/html"}Limitless
Also try this instead RestClient.post 'http://api.example.com/starthere', :auth_token => 'my_token'Limitless
Any information on this question? Did the answers solved the problem anyway? I am facing the same problem here and would like to know if this worked. Thanks.Zelig
R
1

Try using a hash like this:

def start
   url= 'http://api.example.com/starthere'
   params = {auth_token: 'my_token'}.to_json
   response = RestClient.post url, params
   puts response
end
Radack answered 26/8, 2016 at 13:58 Comment(0)
H
0

If you just want to replicate the curl request:

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

Both Curl and RestClient defaults to the same content type (application/x-www-form-urlencoded) when posting data the this format.

Hurter answered 3/2, 2016 at 17:11 Comment(2)
still getting "RestClient::BadRequest: 400 Bad Request"Judenberg
@Judenberg so I tested with your data got the exact same request with my RestClient snippet as with you curl example. Are you sure that you passed the payload as a hash and not a string?Hurter
B
0

In case you land here having the same Issue, Just know that this is a common error that happens when your environment variables are not "set".
I put this in quotes because you might have set it but not available in the current terminal session! You can check if the ENV KEY is available with:

printenv <yourenvkey>

if you get nothing then it means you need to re-add it or just put it in your bash files

FYI: Putting my ENV variables in my ~/.bash_profile fixed it

Bread answered 15/11, 2018 at 4:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.