2

I have the following curl request that I would like to translate into a RestClient API request.

curl -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Authorization: Token user_token="tokenhere", email="email@myemail.com"' -X POST "https://api.myapi.io/reports?start_date=2016-05-10&end_date=2016-05-12" -v

I tried the following as a RestClient POST but I keep getting a 401. When I do the curl request I get a response.

url = "https://api.myapi.io/reports?start_date=2016-05-10&end_date=2016-05-12"

RestClient.post(url, :authorization => 'Token email="email@myemail.com" user_token="tokenhere"', :content_type => 'application/json', :accept => 'application/json')

Any ideas?

Jordan Running
  • 102,619
  • 17
  • 182
  • 182
locoboy
  • 38,002
  • 70
  • 184
  • 260
  • 1
    The `Authorization` header looks different (different order, missing comma). Maybe that's it? – user94559 Jun 14 '16 at 01:28
  • Of course, your `url = ...` line is also missing a quote, so maybe these are just typos. :-) – user94559 Jun 14 '16 at 01:29
  • @smarx is this what you mean? I tried this and it didn't work I got the same response: ```RestClient.post(url, :authorization => 'Token user_token="tokenhere", email="email@myemail.com"', :content_type => 'application/json', :accept => 'application/json')``` – locoboy Jun 14 '16 at 01:31
  • Yup, that's what I meant. What's the body of the HTTP response? Does it give you a hint as to the issue? – user94559 Jun 14 '16 at 01:50
  • That's not the body of the response. That's just the status. – user94559 Jun 14 '16 at 05:54
  • RestClient::Unauthorized: 401 Unauthorized: {"errors":null}. This is the only response that I get from the console. – locoboy Jun 14 '16 at 05:56

1 Answers1

1

The string you're expecting is:

'Token user_token="tokenhere", email="email@myemail.com"'

The line you're sending is:

'Token email="email@myemail.com" user_token="tokenhere"'

The parameters are flipped around. :) If that doesn't work, I'd check and make sure that the curl request is expecting escaped characters. You could be effectively sending this:

"Token email=\"email@myemail.com\" user_token=\"tokenhere\""