2015-01-16 39 views
0

我試圖通過捲曲發送請求到我的Heroku基於API,但我不斷收到此錯誤:HTTP/1.1 401上的捲曲請求未經授權錯誤 - Heroku的

* Hostname was NOT found in DNS cache 
* Trying 23.21.169.234... 
* Connected to helphy-api.herokuapp.com (23.21.169.234) port 80 (#0) 
> POST /users/sign_in HTTP/1.1 
> User-Agent: curl/7.35.0 
> Host: helphy-api.herokuapp.com 
> Accept: */* 
> Authorization: Bearer d5bd07e4-a1c9-46d2-8d8e-d2a7cbc8501f, Accept: application/json 
> Content-Length: 68 
> Content-Type: application/x-www-form-urlencoded 
> 
* upload completely sent off: 68 out of 68 bytes 
< HTTP/1.1 401 Unauthorized 
< Connection: keep-alive 
< X-Frame-Options: SAMEORIGIN 
< X-Xss-Protection: 1; mode=block 
< X-Content-Type-Options: nosniff 
< Content-Type: */*; charset=utf-8 
< Cache-Control: no-cache 
< X-Request-Id: 5b615fce-0674-4302-a9b9-f12cb00db754 
< X-Runtime: 0.005328 
* Server WEBrick/1.3.1 (Ruby/2.0.0/2014-11-13) is not blacklisted 
< Server: WEBrick/1.3.1 (Ruby/2.0.0/2014-11-13) 
< Date: Fri, 16 Jan 2015 21:10:12 GMT 
< Content-Length: 49 
< Via: 1.1 vegur 

這是我的捲髮要求:

curl -v -H "Authorization: Bearer $TUTORIAL_KEY, Accept: application/json" -X POST http://helphy-api.herokuapp.com/users/sign_in -d '{"user": {"email": "[email protected]", "password": "xxxxxxx"}}' 

UPDATE

這是日誌:

2015-01-16T21:43:04.342815+00:00 app[web.1]: Processing by SessionsController#create as */* 
2015-01-16T21:43:04.342822+00:00 app[web.1]: Parameters: {"{\"user\": {\"email\": \"[email protected]\", \"password\": \"xxxxxxx\"}}"=>nil} 
2015-01-16T21:43:04.347678+00:00 app[web.1]: Completed 401 Unauthorized in 5ms 
updating...done. Updated to 3.23.2 

電子郵件和密碼是正確和chequed,順便說一句。

回答

0

-d參數應的X WWW的形式,進行了urlencoded像查詢字符串:

user[email]=email&user[password]=password 

如果你想繼續使用JSON你是你必須將Content-type頭添加的方式你-H

Content-type: application/json 

這讓我們的服務器知道解釋你的POST數據作爲JSON,而不是X WWW的形式,進行了urlencoded字符串。這就是爲什麼在服務器日誌中的參數出來,如:

Parameters: {"{\"user\": {\"email\": \"[email protected]\", \"password\": \"xxxxxxx\"}}"=>nil} 

這只是一個字符串,而不是JSON,因爲服務器不知道你在發送JSON,因爲你沒有包含內容 - 類型標題和默認服務器假定POST數據是一個x-www-form-urlencoded字符串。

相關問題