xref: /curl/docs/libcurl/opts/CURLOPT_POSTREDIR.md (revision e3fe0200)
1---
2c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
3SPDX-License-Identifier: curl
4Title: CURLOPT_POSTREDIR
5Section: 3
6Source: libcurl
7See-also:
8  - CURLINFO_EFFECTIVE_METHOD (3)
9  - CURLINFO_REDIRECT_COUNT (3)
10  - CURLOPT_FOLLOWLOCATION (3)
11  - CURLOPT_MAXREDIRS (3)
12  - CURLOPT_POSTFIELDS (3)
13Protocol:
14  - HTTP
15---
16
17# NAME
18
19CURLOPT_POSTREDIR - how to act on an HTTP POST redirect
20
21# SYNOPSIS
22
23~~~c
24#include <curl/curl.h>
25
26CURLcode curl_easy_setopt(CURL *handle, CURLOPT_POSTREDIR,
27                          long bitmask);
28~~~
29
30# DESCRIPTION
31
32Pass a bitmask to control how libcurl acts on redirects after POSTs that get a
33301, 302 or 303 response back. A parameter with bit 0 set (value
34**CURL_REDIR_POST_301**) tells the library to respect RFC 7231 (section
356.4.2 to 6.4.4) and not convert POST requests into GET requests when following
36a 301 redirection. Setting bit 1 (value **CURL_REDIR_POST_302**) makes
37libcurl maintain the request method after a 302 redirect whilst setting bit 2
38(value **CURL_REDIR_POST_303**) makes libcurl maintain the request method
39after a 303 redirect. The value **CURL_REDIR_POST_ALL** is a convenience
40define that sets all three bits.
41
42The non-RFC behavior is ubiquitous in web browsers, so the library does the
43conversion by default to maintain consistency. However, a server may require a
44POST to remain a POST after such a redirection. This option is meaningful only
45when setting CURLOPT_FOLLOWLOCATION(3).
46
47# DEFAULT
48
490
50
51# EXAMPLE
52
53~~~c
54int main(void)
55{
56  CURL *curl = curl_easy_init();
57  if(curl) {
58    curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
59
60    /* a silly POST example */
61    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "data=true");
62
63    /* example.com is redirected, so we tell libcurl to send POST on 301,
64       302 and 303 HTTP response codes */
65    curl_easy_setopt(curl, CURLOPT_POSTREDIR, CURL_REDIR_POST_ALL);
66
67    curl_easy_perform(curl);
68  }
69}
70~~~
71
72# AVAILABILITY
73
74Added in 7.17.1. This option was known as CURLOPT_POST301 up to 7.19.0 as it
75only supported the 301 then. CURL_REDIR_POST_303 was added in 7.26.0.
76
77# RETURN VALUE
78
79Returns CURLE_OK if the option is supported, and CURLE_UNKNOWN_OPTION if not.
80