使用RestClient和Authentication标头发送POST请求

时间:2016-06-14 01:25:52

标签: ruby rest-client

我有以下curl请求,我想将其转换为RestClient API请求。

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

我尝试了以下作为RestClient POST,但我一直得到401.当我做curl请求时,我收到了回复。

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')

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您期待的字符串是:

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

您发送的信息是:

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

参数被翻转。 :)如果这不起作用,我会检查并确保curl请求期望转义字符。你可以有效地发送这个:

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