2015-04-14 73 views
2

我正在使用rest-client gem嘗試向我的API發佈帖子。rest-client gem:401未經授權使用令牌

RestClient.post幫助程序需要三個參數來傳遞標題,如:.post(url,params,headers)。你有沒有嘗試過更類似的東西?

RestClient.post( 'http://api.example.com/',{鍵: '值'},授權: 'A2M ...')

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

我按照上面的建議,但收到RestClient::Unauthorized - 401 Unauthorized作爲響應。

我的代碼:

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

我有下面的curl命令而不是上面的RestClient.post成功。成功捲曲:

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

回答

2

這應當產生捲曲相同的請求:

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

感謝。我認爲在使用':authorization'時,rest-client會自動添加「Token token =」。我錯了,你的回答爲我解決了這個問題。 – Marklar

相關問題