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
Tom
  • 9,275
  • 25
  • 89
  • 147
  • Are you sure that your request is `text/plain` request? – Pavan Feb 03 '16 at 12:03
  • @Pavan curl works so I guess it is right? 'curl "http://api.example.com/starthere" -d "auth_token=my_token"' – Tom Feb 03 '16 at 12:06
  • How are you handling the request at the controller? Can you post the related code? – Pavan Feb 03 '16 at 12:12
  • @Pavan I'm simply printing it to console to see the response as of now – Tom Feb 03 '16 at 12:15
  • Try changing `{"Content-Type" => "text/plain"}` to `{"Content-Type" => "text/html"}` – Pavan Feb 03 '16 at 12:18
  • Also try this instead `RestClient.post 'http://api.example.com/starthere', :auth_token => 'my_token'` – Pavan Feb 03 '16 at 12:24
  • 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. – Ed de Almeida Feb 23 '17 at 16:52

3 Answers3

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
zlwaterfield
  • 841
  • 1
  • 9
  • 18
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.

Pafjo
  • 4,979
  • 3
  • 23
  • 31
  • still getting "RestClient::BadRequest: 400 Bad Request" – Tom Feb 04 '16 at 02:39
  • @Tom 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? – Pafjo Feb 04 '16 at 05:07
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

Eyewritecode
  • 151
  • 1
  • 8