Ruby 发送带有RestClient和身份验证头的POST请求
我有以下curl请求,我想将其转换为restclientapi请求Ruby 发送带有RestClient和身份验证头的POST请求,ruby,rest-client,Ruby,Rest Client,我有以下curl请求,我想将其转换为restclientapi请求 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_d
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帖子,但我一直得到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')
有什么想法吗?您期望的字符串是:
'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\""
授权
标题看起来不同(顺序不同,缺少逗号)。也许就是这样?当然,您的url=…
行也缺少一个引号,所以这些可能只是打字错误。:-)@这就是你的意思吗?我尝试了这个,但没有成功,我得到了相同的响应:RestClient.post(url,:authorization=>“Token user\u Token=“tokenhere”,email=”email@myemail.com“,:content_type=>'application/json',:accept=>'application/json')
是的,这就是我的意思。HTTP响应的主体是什么?关于这个问题,它给了你一个暗示吗?这不是回答的主体。这只是状态。我如何检查请求是否需要转义字符?