Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails RestClient pass token in header fails

I do this curl in Terminal which works excellent:

$ curl https://myurl.com/api/v1/orders/53e0ae7f6630361c46060000 -H "Authorization: Token xxxxxxxxxxxxxxxxxxxxxx"

Output is json.

Now I want to access the json string via my rails app. I have tried RestClient to do this, but somehow I always get a 401 unauthorized error. I believe the token gets not send correctly via header. I have tried the following:

RestClient.get 'https://myurl.com/api/v1/orders/53e0ae7f6630361c46060000', {token: 'xxxxxxxxxxxxxxxxxxxxxx'}

and

RestClient.get 'https://myurl.com/api/v1/orders/53e0ae7f6630361c46060000', :params => {:token => 'xxxxxxxxxxxxxxxxxxxxxx'}

with no success. Maybe I use a wrong syntax for sending the token in the header? Doku is here http://rubydoc.info/github/rest-client/rest-client - I could not find any mistakes.

like image 563
DonMB Avatar asked Oct 14 '25 16:10

DonMB


1 Answers

# GET request with modified headers
RestClient.get 'http://example.com/resource', {:Authorization => 'Bearer cT0febFoD5lxAlNAXHo6g'}

# POST request with modified headers
RestClient.post 'http://example.com/resource', {:foo => 'bar', :baz => 'qux'}, {:Authorization => 'Bearer cT0febFoD5lxAlNAXHo6g'}

# DELETE request with modified headers
RestClient.delete 'http://example.com/resource', {:Authorization => 'Bearer cT0febFoD5lxAlNAXHo6g'}

Source: https://github.com/rest-client/rest-client#headers

like image 108
Fran Martinez Avatar answered Oct 17 '25 08:10

Fran Martinez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!