Menu

POST –> redirect –> GET

11 października 2018 - IT, Network
POST –> redirect –> GET

Redirect dla metody POST za pomocą 301-303 powoduje wysłanie do nowej lokalizacji zapytania GET.
Przykład dla przekierowania 303:

curl -vL -X POST http://example.com/example -H 'Authorization: Basic SDRFGzsdrgsDEDZFHBR=' -H 'Cache-Control: no-cache' \
 -H 'Content-Type: application/json' -d '{"example-data"}' 
*   Trying XXX.XXX.XXX.XXX... 
* Connected to example.com (XXX.XXX.XXX.XXX) port 80 (#0) 
> POST /example HTTP/1.1 
> Host: example.com
> User-Agent: curl/7.47.0 
> Accept: */* 
> Authorization: Basic SDRFGzsdrgsDEDZFHBR=
> Cache-Control: no-cache 
> Content-Type: application/json 
> Content-Length: 582 
>  
* upload completely sent off: 582 out of 582 bytes 
< HTTP/1.1 303 See Other 
< Cache-Control: no-cache 
< Content-length: 0 
< Location: https://example.com/example 
< Connection: close 
<  
* Closing connection 0 
* Issue another request to this URL: 'https://example.com/example' 
* Disables POST, goes with GET 
*   Trying...

Przykład dla przekierowania 301:
* upload completely sent off: 582 out of 582 bytes
< HTTP/1.1 301 Moved Permanently
< Content-length: 0
< Location: https://example.com/example
< Connection: close
<  
* Closing connection 0
* Issue another request to this URL: 'https://example.com/example'
* Switch from POST to GET

Przykład dla przekierowania 302:
* upload completely sent off: 582 out of 582 bytes 
< HTTP/1.1 302 Found 
< Cache-Control: no-cache 
< Content-length: 0 
< Location: https://example.com/example 
< Connection: close 
<  
* Closing connection 0 
* Issue another request to this URL: 'https://example.com/example' 
* Switch from POST to GET 

Rozwiązaniem działającym jest zastosowanie przekierowania 307:

* upload completely sent off: 582 out of 582 bytes
< HTTP/1.1 307 Temporary Redirect
< Cache-Control: no-cache
< Content-length: 0
< Location: https://example.com/example
< Connection: close
<  
* Closing connection 0
* Issue another request to this URL: 'https://example.com/example'
*   Trying...

 

Źródła:

https://tools.ietf.org/html/rfc7231#section-6.4