简体   繁体   English

烧瓶中没有POST请求和Content-Type“application / json”的响应

[英]No response with POST request and Content-Type “application/json” in flask

I'm having problems with a Flask view that should return a response with content-type "application/json" in response to a POST request. 我遇到了Flask视图的问题,该视图应该返回内容类型为“application / json”的响应以响应POST请求。 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) 我得到了预期的完整回复(200-ok)

{"test": "ok"}

By the way, if I send a GET request to the same JSON route: 顺便说一下,如果我向同一个JSON路由发送GET请求:

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. 感谢Audrius的评论,我跟踪了uWSGI和nginx之间交互的问题的可能来源:显然,如果你在请求中收到POST数据,你必须在返回响应之前阅读它。

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 . 根据uWSGI的作者Roberto De Ioris的描述,一个不同的解决方案包括将--post --post-buffering 1传递给uWSGI

I still don't understand why the problem does not present itself with Content-Type set to "text/html" 我仍然不明白为什么问题不会出现, Content-Type设置为"text/html"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flask-Restful 错误:请求 Content-Type 不是 'application/json'。"} - Flask-Restful Error: request Content-Type was not 'application/json'."} 如何将请求和响应“Content-Type”设置为“application/json;charset=UTF-8”? - How to set Request and response “Content-Type” to “application/json;charset=UTF-8”? Flask请求和application / json内容类型 - Flask request and application/json content type 在标头中设置Content-Type / application / json - Setting Content-Type / application/json in header 强制 Content-Type 或公开 Flask 中已知内容类型的 request.data - Force Content-Type or expose request.data in Flask for known content-type Python 请求不发送 {&quot;Content-Type&quot;:&quot;application/json&quot;} 标头或只是忽略 HTTP POST 中的正文 - Python requests not sending {"Content-Type":"application/json"} header or is just ignoring body in HTTP POST 如何从带有jsonapi版本1.0,“ Content-type” =“ application / vnd.api + json”的Flask应用中获取有效的json resp - How to get valid json resp from Flask app with jsonapi version 1.0, “Content-type”=“application/vnd.api+json” 使用Multipart / form-data的POST请求。内容类型不正确 - POST request with Multipart/form-data. Content-type not correct 根据 Flask 中请求的内容类型更改响应 - Change response based on content type of request in Flask 无法使用urllib2将content-type设置为application / json - Not possible to set content-type to application/json using urllib2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM