用Flask设计了一个api接口
115.29.173.126:21084/runningfast/api/v1.0/open/register
传入:
{ "email": "[email protected]", "facebookUserId": "1577xxx4", "firstName": "mxxx", "lastName": "cxx", "password": "111111", "phone": "18xxxxxx", "smsCode": "325721" } |
用postman测试,可以正常返回:
{ "code": 200, "data": { "created_at": "2016-10-12T11:14:35", "email": "[email protected]", "facebookUserId": "157xxx04", "firstName": "mxxx", "id": "user-4ddeb6b9-27cb-44d1-a0be-222ade2470ec", "lastName": "cxxx", "modified_at": "2016-10-12T11:14:35", "password": "111111", "phone": "18xxxxx" }, "message": "new user has created" } |
如图:
但是android端用xutil3去测试,结果却返回:
errorCode: 400, msg: Bad Request, result: <html> <head> <title>Bad Request</title> </head> <body> <h1><p>Bad Request</p></h1> Invalid HTTP Version ‘Invalid HTTP Version: ‘/v1.0/open/register HTTP/1.1” </body> </html> |
flask rest api Invalid HTTP Version HTTP/1.1
flask Bad Request Invalid HTTP Version HTTP/1.1
flask-restful Invalid HTTP Version
xutil Invalid HTTP Version HTTP/1.1
xutil Invalid HTTP Version
Key Differences between HTTP/1.0 and HTTP/1.1
[Bro] event "Invalid HTTP version string" ?
Invalid HTTP Version
flask-restful http version
尝试用curl测试自己的api试试
{"email":"[email protected]","facebookUserId":"15xxx4","firstName":"l","lastName":"bj","password":"111111","phone":"18xxxxx","smsCode":"157341"} |
curl http://115.29.173.126:21084/runningfast/api/v1.0/open/register -X POST -v -d "{"email":"[email protected]","facebookUserId":"157731344684904","firstName":"l","lastName":"bj","password":"111111","phone":"18790639671","smsCode":"157341"}"
结果很奇怪的报错手机号不能为空:
➜ ~ curl http://115.29.173.126:21084/runningfast/api/v1.0/open/register -X POST -v -d "{"email":"[email protected]","facebookUserId":"15xxx4","firstName":"l","lastName":"bj","password":"111111","phone":"18xxxx","smsCode":"157341"}" Note: Unnecessary use of -X or –request, POST is already inferred. * Trying 115.29.173.126… * Connected to 115.29.173.126 (115.29.173.126) port 21084 (#0) > POST /runningfast/api/v1.0/open/register HTTP/1.1 > Host: 115.29.173.126:21084 > User-Agent: curl/7.49.1 > Accept: */* > Content-Length: 129 > Content-Type: application/x-www-form-urlencoded > * upload completely sent off: 129 out of 129 bytes < HTTP/1.1 200 OK < Server: gunicorn/19.6.0 < Date: Wed, 12 Oct 2016 04:00:47 GMT < Connection: close < Content-Type: application/json < Content-Length: 64 < { "code": 10101, "message": "phone can not be empty" } * Closing connection 0 |
是可以的。
➜ ~ curl http://115.29.173.126:21084/runningfast/api/v1.0/open/register -X POST -v -d {"email":"[email protected]","facebookUserId":"15xxx4","firstName":"l","lastName":"bj","password":"111111","phone":"18xxxx","smsCode":"157341"} Note: Unnecessary use of -X or –request, POST is already inferred. * Trying 115.29.173.126… * Connected to 115.29.173.126 (115.29.173.126) port 21084 (#0) > POST /runningfast/api/v1.0/open/register HTTP/1.1 > Host: 115.29.173.126:21084 > User-Agent: curl/7.49.1 > Accept: */* > Content-Length: 23 > Content-Type: application/x-www-form-urlencoded > * upload completely sent off: 23 out of 23 bytes < HTTP/1.1 200 OK < Server: gunicorn/19.6.0 < Date: Wed, 12 Oct 2016 06:12:31 GMT < Connection: close < Content-Type: application/json < Content-Length: 64 < { "code": 10101, "message": "phone can not be empty" } * Closing connection 0 Note: Unnecessary use of -X or –request, POST is already inferred. * Rebuilt URL to: facebookUserId:157731344684904/ * Port number out of range * Closing connection -1 curl: (3) Port number out of range Note: Unnecessary use of -X or –request, POST is already inferred. * Rebuilt URL to: firstName:l/ ^C |
【已解决】curl去POST传JSON失败:Failed to decode JSON object Expecting property name enclosed in double quotes
最后的最后,发现是:
android端,在生成api接口时,中间多了个空格:
http://115.29.173.126:21084/runningfast/api/ v1.0/open/register
去掉空格:
http://115.29.173.126:21084/runningfast/api/v1.0/open/register
就可以正常访问接口了。。。
【总结】
此处Flask的restfule的api接口,android端访问,由于把api地址不小心多弄了个空格:
http://115.29.173.126:21084/runningfast/api/ v1.0/open/register
去掉空格:
http://115.29.173.126:21084/runningfast/api/v1.0/open/register
就可以了。
转载请注明:在路上 » 【已解决】Flask的rest的api返回出错:Invalid HTTP Version HTTP/1.1