2

ユーザーが新しいオブジェクトを作成するためにデータを送信した後、通常どおり、バックエンドがユーザーをオブジェクトの詳細にリダイレクトするようにします。

私は次のようなことをしています:

if obj.is_valid():
    obj.save()
    flash('%s created' % obj, )
    return redirect(url_for('provider', provider_id=obj.id))

しかし、curlを介してリクエストを実行します(私はAPIを構築しており、curlでテストしています)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>Redirecting...</title>
<h1>Redirecting...</h1>
<p>You should be redirected automatically to target URL: <a href="/provider/11">/provider/11</a>.  If not click the link.
user@box

/provider/:id中間ステップではなく、応答だけが必要なので、これは残念です。

この問題に対処する正しい方法は何ですか?

4

4 に答える 4

4

実際、Flaskは、 RFC2616セクション10.3.2で定義されているようにここで正しいことを行っています。

新しい永続的なURIは、応答のLocationフィールドで指定する必要があります。リクエストメソッドがHEADでない限り、レスポンスのエンティティには、新しいURIへのハイパーリンクを含む短いハイパーテキストメモが含まれている必要があります。

[強調鉱山]

RFC 2119を引用すると、この段落は次のように言い換えることができます。

HTTPサーバーには、それを回避する非常に正当な理由がない限り、 30Xリダイレクトを含む短いハイパーテキストメモを含める必要があります

ブラウザや一般的なHTTPライブラリを使用している人は誰もそのページを見ることができません。クライアントは、LocationRFC2616の関連セクションで指定されているヘッダーで提供されるリダイレクトに従います。

ペンダンティック講義はさておき、JSON APIを構築していて、ステータスコードと必要なヘッダー以外は何も提供したくない場合は、Flask.errorhandlerデコレータまたはFlask.register_error_handler:を使用して適切なメソッドに独自のエラーハンドラを登録することでそれを行うことができます。

@app.errorhandler(302)
def minimal_redirect():
    return u"", 302
于 2012-07-31T01:31:44.717 に答える
3

-LHTTPリダイレクトに従う必要があることをcurlに指示し、最終応答のみを表示するために使用します。

$ curl -s http://www.exyr.org|grep '<title>' 
<title>301 Moved Permanently</title>
$ curl -s -L http://www.exyr.org|grep '<title>'
  <title>Exyr.org</title>

man curl

   -L, --location
          (HTTP/HTTPS)  If  the server reports that the requested page has
          moved to a different location (indicated with a Location: header
          and  a  3XX  response code), this option will make curl redo the
          request on the new place. If used together with -i, --include or
          -I, --head, headers from all requested pages will be shown. When
          authentication is used, curl only sends its credentials  to  the
          initial  host.  If a redirect takes curl to a different host, it
          won't be able to intercept the user+password. See  also  --loca‐
          tion-trusted  on how to change this. You can limit the amount of
          redirects to follow by using the --max-redirs option.

          When curl follows a redirect and the request is not a plain  GET
          (for example POST or PUT), it will do the following request with
          a GET if the HTTP response was 301, 302, or 303. If the response
          code  was  any  other  3xx code, curl will re-send the following
          request using the same unmodified method.
于 2012-07-31T09:16:58.120 に答える
1

フラスコが宛先自体にリダイレクトする前にリダイレクトページにリダイレクトしたので、私は同様のクエリを持っていました。

code実際に問題を解決したのは削除するだけです。ごとにカスタムルート/ハンドラーを指定することもできますcode

redirect(url_for('home'), code=302)

に、

redirect(url_for('home'))

または、@ Sean Vieiraが指摘したように、このデコレータを使用してリダイレクトを処理します

@app.errorhandler(302)
def custom_redirect():
    # Return template
于 2019-05-28T05:04:48.463 に答える
0

redirect応答のHTTPボディを簡単に削除します。

res = redirect(url_for('provider', provider_id=obj.id))
res.data = ""
return res
于 2012-07-30T19:31:47.677 に答える