没有响应 POST 请求和 Content-Type “application/json";在烧瓶中 [英] No response with POST request and Content-Type "application/json" in flask

查看:34
本文介绍了没有响应 POST 请求和 Content-Type “application/json";在烧瓶中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在处理 Flask 视图时遇到问题,该视图应该返回内容类型为application/json"的响应以响应 POST 请求.具体来说,如果我这样做:

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

到这个视图:

@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

如果我这样做:

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)

I get the expected the full response (200-ok)

{"test": "ok"}

顺便说一下,如果我向同一个 JSON 路由发送 GET 请求:

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?

推荐答案

感谢 Audrius 的评论,我跟踪了 uWSGI 和 nginx 之间交互的可能问题根源:显然,如果您在请求中收到 POST 数据,您必须在返回响应之前阅读它.

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

另一种解决方案是将 --post-buffering 1 传递给 uWSGI,如 uWSGI 的作者 Roberto De 所述鸢尾花.

A different solution involves passing --post-buffering 1 to uWSGI as described by uWSGI's author Roberto De Ioris.

我仍然不明白为什么在 Content-Type 设置为 "text/html"

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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆