Ответы пользователя по тегу Django Rest Framework
  • Как изменить метод при redirect() если метод PUT?

    @breaden
    Дефолтное поведение предполагает (rfc7231) следующее:

    Если POST запрос возвращается с кодом 301, в таком случае follow_redirect приведет к GET запросу нового адреса.
    В случае curl, это происходит автоматически, при наличии флага -L и пейлоада (без явного указания типа запроса) (аналогично allow_redirect=True в requests)

    В данном примере, есть вьюха POST /test возвращающая HttpResponsePermanentRedirect (с кодом 301), с редиректом на /test2.
    curl 'http://127.0.0.1:8000/test' -v -L -d '{}'
    *   Trying 127.0.0.1:8000...
    * Connected to 127.0.0.1 (127.0.0.1) port 8000 (#0)
    > POST /test HTTP/1.1
    > Host: 127.0.0.1:8000
    > User-Agent: curl/7.79.1
    > Accept: */*
    > Content-Length: 2
    > Content-Type: application/x-www-form-urlencoded
    >
    * Mark bundle as not supporting multiuse
    < HTTP/1.1 301 Moved Permanently
    < Date: Mon, 01 May 2023 14:10:33 GMT
    < Server: WSGIServer/0.2 CPython/3.9.6
    < Content-Type: text/html; charset=utf-8
    < Location: test2
    < Vary: Accept, Cookie
    < Allow: OPTIONS, POST
    < X-Frame-Options: DENY
    < Content-Length: 0
    < X-Content-Type-Options: nosniff
    < Referrer-Policy: same-origin
    < Cross-Origin-Opener-Policy: same-origin
    <
    * Connection #0 to host 127.0.0.1 left intact
    * Issue another request to this URL: 'http://127.0.0.1:8000/test2'
    * Switch from POST to GET
    * Found bundle for host 127.0.0.1: 0x600003424030 [serially]
    * Can not multiplex, even if we wanted to!
    * Re-using existing connection! (#0) with host 127.0.0.1
    * Connected to 127.0.0.1 (127.0.0.1) port 8000 (#0)
    > GET /test2 HTTP/1.1
    > Host: 127.0.0.1:8000
    > User-Agent: curl/7.79.1
    > Accept: */*
    >
    * Mark bundle as not supporting multiuse
    < HTTP/1.1 200 OK
    < Date: Mon, 01 May 2023 14:10:33 GMT
    < Server: WSGIServer/0.2 CPython/3.9.6
    < Content-Type: application/json
    < Vary: Accept, Cookie
    < Allow: OPTIONS, GET
    < X-Frame-Options: DENY
    < Content-Length: 22
    < X-Content-Type-Options: nosniff
    < Referrer-Policy: same-origin
    < Cross-Origin-Opener-Policy: same-origin
    <
    * Connection #0 to host 127.0.0.1 left intact
    {"redirect":"success"}


    По хорошему, тип запроса при редиректе меняться не должен. То есть, PUT не должен резко менять своё поведение на GET в случае редиректа.
    Ответ написан
    Комментировать