Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails HTTP/1.1 401 curl请求上的未授权错误-heroku_Ruby On Rails_Api_Curl_Heroku_Httprequest - Fatal编程技术网

Ruby on rails HTTP/1.1 401 curl请求上的未授权错误-heroku

Ruby on rails HTTP/1.1 401 curl请求上的未授权错误-heroku,ruby-on-rails,api,curl,heroku,httprequest,Ruby On Rails,Api,Curl,Heroku,Httprequest,我试图通过curl向我基于heroku的api发送请求,但我一直收到以下错误: * 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: h

我试图通过curl向我基于heroku的api发送请求,但我一直收到以下错误:

* 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
更新

这是日志:

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\": \"xxxxxx@gmail.com\", \"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

电子邮件和密码正确无误且已打勾,顺便说一句。

您的
-d
参数应为x-www-form-url,编码方式与查询字符串类似:

user[email]=email&user[password]=password
如果要继续使用JSON,必须将内容类型标题添加到
-H

Content-type: application/json
这让服务器知道如何将POST数据解释为JSON,而不是x-www-form-urlencoded字符串。这就是为什么服务器日志中的参数显示为:

Parameters: {"{\"user\": {\"email\": \"xxxxxx@gmail.com\", \"password\": \"xxxxxxx\"}}"=>nil}
这只是一个字符串,而不是JSON,因为服务器不知道您正在发送JSON,因为您没有包含内容类型头,并且默认情况下,服务器假定POST数据是一个x-www-form-urlencoded字符串

Parameters: {"{\"user\": {\"email\": \"xxxxxx@gmail.com\", \"password\": \"xxxxxxx\"}}"=>nil}