POST请求和Content-Type“application / json”没有响应在烧瓶中 [英] No response with POST request and Content-Type "application/json" in flask
问题描述
我对Flask视图有问题,应该返回一个响应POST请求的内容类型为application / json的响应。
具体来说,如果我这样做:
$ $ $ $ $ $ $ curl -v -d'foo = bar'http://示例。 org / jsonpost
到这个视图:
@ app.route('/ jsonpost',methods = ['GET','POST'])
def json_post():
resp = make_response {test:ok}')
resp.headers ['Content-Type'] =application / json
return resp
pre>
我得到某种连接重置:
*关于连接()到example.org端口80(#0)
*尝试xxx.xxx.xxx.xxx ...连接
*连接到example.org(xxx.xxx.xxx.xxx)端口80(#0)
> POST / routing / jsonpost HTTP / 1.1
> User-Agent:curl / 7.19.7(i486-pc-linux-gnu)libcurl / 7.19.7 OpenSSL / 0.9.8k zlib / 1.2.3.3 libidn / 1.15
>主机:example.org
>接受:* / *
> Content-Length:7
> Content-Type:application / x-www-form-urlencoded
>
< HTTP / 1.1 200 OK
<服务器:nginx / 1.2.4
<日期:2012年12月27日星期四14:07:59 GMT
< Content-Type:application / json
<内容长度:14
<连接:保持活动
< Set-Cookie:session =...;路径= /; HttpOnly
< Cache-Control:public
<
*传输关闭,剩余14个字节读取
*关闭连接#0
卷曲:(18)传输关闭,剩余14个字节读取
如果我这样做:
curl -d 'foo = bar'http://example.org/htmlpost
到:
@ app.route('/ htmlpost',methods = ['GET','POST'])
def html_post()
resp = make_response('{test:ok}')
resp.headers ['Content-Type'] =text / html
return resp
我得到预期的完整回复(200-ok)
{test:ok}
如果我发送GET请求到相同的JSON路由:
curl http://example.org/jsonpost
我也得到预期的回应..
有什么想法吗?
解决方案感谢Audrius的评论I跟踪了uWSGI和nginx之间的交互问题的一个可能的来源:显然,如果您在请求中收到POST数据,您必须在返回响应之前将其读取。
例如,这可以解决我的问题。
@ app.route('/ jsonpost',methods =''GET','POST'])
def json_post():
if request.method =='POST':
dummy = request.form
resp = make_response ('{test:ok}')
resp.headers ['Content-Type'] =application / json
return resp
另一个解决方案是将
- post-buffering 1
传递给uWSGI,如 uWSGI的作者Roberto De Ioris 。
我还是不明白为什么问题不会以
Content-Type
设置为text / html
I'm having problems with a Flask view that should return a response with content-type "application/json" in response to a POST request. Specifically, if I do:
curl -v -d 'foo=bar' http://example.org/jsonpost
to this view:
@app.route('/jsonpost', methods=['GET', 'POST']) def json_post(): resp = make_response('{"test": "ok"}') resp.headers['Content-Type'] = "application/json" return resp
I get some sort of connection reset:
* About to connect() to example.org port 80 (#0) * Trying xxx.xxx.xxx.xxx... connected * Connected to example.org (xxx.xxx.xxx.xxx) port 80 (#0) > POST /routing/jsonpost HTTP/1.1 > User-Agent: curl/7.19.7 (i486-pc-linux-gnu) libcurl/7.19.7 OpenSSL/0.9.8k zlib/1.2.3.3 libidn/1.15 > Host: example.org > Accept: */* > Content-Length: 7 > Content-Type: application/x-www-form-urlencoded > < HTTP/1.1 200 OK < Server: nginx/1.2.4 < Date: Thu, 27 Dec 2012 14:07:59 GMT < Content-Type: application/json < Content-Length: 14 < Connection: keep-alive < Set-Cookie: session="..."; Path=/; HttpOnly < Cache-Control: public < * transfer closed with 14 bytes remaining to read * Closing connection #0 curl: (18) transfer closed with 14 bytes remaining to read
If instead I do:
curl -d 'foo=bar' http://example.org/htmlpost
to:
@app.route('/htmlpost', methods=['GET', 'POST']) def html_post(): resp = make_response('{"test": "ok"}') resp.headers['Content-Type'] = "text/html" return resp
I get the expected the full response (200-ok)
{"test": "ok"}
By the way, if I send a GET request to the same JSON route:
curl http://example.org/jsonpost
I also get the expected response.. Any ideas?
解决方案Thanks to Audrius's comments I tracked a possible source of the problem to the interaction between uWSGI and nginx: apparently, if you receive POST data in a request you must read it before returning a response.
This, for example, fixes my issue.
@app.route('/jsonpost', methods=['GET', 'POST']) def json_post(): if request.method == 'POST': dummy = request.form resp = make_response('{"test": "ok"}') resp.headers['Content-Type'] = "application/json" return resp
A different solution involves passing
--post-buffering 1
to uWSGI as described by uWSGI's author Roberto De Ioris.I still don't understand why the problem does not present itself with
Content-Type
set to"text/html"
这篇关于POST请求和Content-Type“application / json”没有响应在烧瓶中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!