Search code examples
rest-client

rest-client gem: 401 unauthorized with token


I am using the rest-client gem to try and make a post to my API.

The RestClient.post helper requires three arguments to pass headers like that: .post(url, params, headers). Have you tried something more like this?

RestClient.post('http://api.example.com/', {key: 'value'}, authorization: 'a2m...')

https://github.com/rest-client/rest-client/issues/339#issuecomment-71787018

I have followed the advice above but receive RestClient::Unauthorized - 401 Unauthorized as the response.

My code:

RestClient.post "http://api.example-dev.com:7000/v1/resources", {key: 'value'}, :authorization => 'yyyyyyyy'

I have success with the below curl command but not the above RestClient.post. Successful curl:

curl -i -X POST -d 'test[key]=1234' -H "Authorization: Token token=yyyyyyyyyyyyyy" \ http://api.example-dev.com:7000/v1/resources

Solution

  • This should produce the same request as curl:

    `RestClient.post "http://api.example-dev.com:7000/v1/resources", {:test => {key: '1234'}}, :authorization => 'Token token=yyyyyyyyyyyyyy'`