Rails RestClient POST request failing with “400 Bad Request”

限于喜欢 提交于 2019-12-05 11:16:04

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

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!