4

POSTリクエストに応答してコンテンツタイプ「application/json」のレスポンスを返す必要があるFlaskビューに問題があります。具体的には、私が行う場合:

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

ある種の接続リセットが発生します:

* 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)

{"test": "ok"}

ちなみに、同じJSONルートにGETリクエストを送信すると、次のようになります。

curl http://example.org/jsonpost

期待通りの反応も得られます。何かアイデアはありますか?

4

1 に答える 1

13

Audriusのコメントのおかげで、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

別の解決策は、 uWSGIの作者であるRobertoDeIoris--post-buffering 1によって説明されているようにuWSGIに渡すことです。

Content-Typeなぜ問題がに設定されて現れないのか私はまだ理解していません"text/html"

于 2012-12-27T21:11:23.973 に答える