CURLOPT_FOLLOWLOCATION.md (5488B)
1 --- 2 c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 3 SPDX-License-Identifier: curl 4 Title: CURLOPT_FOLLOWLOCATION 5 Section: 3 6 Source: libcurl 7 See-also: 8 - CURLINFO_REDIRECT_COUNT (3) 9 - CURLINFO_REDIRECT_URL (3) 10 - CURLOPT_POSTREDIR (3) 11 - CURLOPT_PROTOCOLS_STR (3) 12 - CURLOPT_REDIR_PROTOCOLS_STR (3) 13 - CURLOPT_UNRESTRICTED_AUTH (3) 14 Protocol: 15 - HTTP 16 Added-in: 7.1 17 --- 18 19 # NAME 20 21 CURLOPT_FOLLOWLOCATION - follow HTTP 3xx redirects 22 23 # SYNOPSIS 24 25 ~~~c 26 #include <curl/curl.h> 27 28 CURLcode curl_easy_setopt(CURL *handle, CURLOPT_FOLLOWLOCATION, long mode); 29 ~~~ 30 31 # DESCRIPTION 32 33 This option tells the library to follow `Location:` header redirects that an 34 HTTP server sends in a 30x response. The `Location:` header can specify a 35 relative or an absolute URL to follow. The long parameter *mode* instructs how 36 libcurl should act on subsequent requests. 37 38 *mode* only had a single value (1L) for a long time that enables redirect 39 following. Since 8.13.0, two additional modes are also supported. See below. 40 41 When following redirects, libcurl issues another request for the new URL and 42 follows subsequent new `Location:` redirects all the way until no more such 43 headers are returned or the maximum limit is reached. CURLOPT_MAXREDIRS(3) is 44 used to limit the number of redirects libcurl follows. 45 46 libcurl restricts what protocols it automatically follow redirects to. The 47 accepted target protocols are set with CURLOPT_REDIR_PROTOCOLS_STR(3). By 48 default libcurl allows HTTP, HTTPS, FTP and FTPS on redirects. 49 50 When following a redirect, the specific 30x response code also dictates which 51 request method libcurl uses in the subsequent request: For 301, 302 and 303 52 responses libcurl switches method from POST to GET unless CURLOPT_POSTREDIR(3) 53 instructs libcurl otherwise. All other redirect response codes make libcurl 54 use the same method again. 55 56 When libcurl switches method to GET, it then uses that method without sending 57 any request body. If it does not change the method, it sends the subsequent 58 request the same way as the previous one; including the request body if one 59 was provided. 60 61 For users who think the existing location following is too naive, too simple 62 or just lacks features, it is easy to instead implement your own redirect 63 follow logic with the use of curl_easy_getinfo(3)'s CURLINFO_REDIRECT_URL(3) 64 option instead of using CURLOPT_FOLLOWLOCATION(3). 65 66 By default, libcurl only sends `Authentication:` or explicitly set `Cookie:` 67 headers to the initial host given in the original URL, to avoid leaking 68 username + password to other sites. CURLOPT_UNRESTRICTED_AUTH(3) is provided 69 to change that behavior. 70 71 Due to the way HTTP works, almost any header can be made to contain data a 72 client may not want to pass on to other servers than the initially intended 73 host and for all other headers than the two mentioned above, there is no 74 protection from this happening when libcurl is told to follow redirects. 75 76 Pick one of the following modes: 77 78 ## CURLFOLLOW_ALL (1) 79 80 Before 8.13.0 this bit had no name and 1L was just the value to enable this 81 option. This makes a set custom method be used in all HTTP requests, even 82 after redirects. 83 84 ## CURLFOLLOW_OBEYCODE (2) 85 86 When there is a custom request method set with CURLOPT_CUSTOMREQUEST(3), that 87 set method replaces what libcurl would otherwise use. If a 301/302/303 88 response code is returned to signal a redirect, the method is changed from 89 POST to `GET`. For 307/308, the custom method remains set and used. 90 91 Note that only POST (or a custom post) is changed to GET on 301/302, its not 92 change PUT etc - and therefore also not when libcurl issues a custom PUT. A 93 303 response makes it switch to GET independently of the original method 94 (except for HEAD). 95 96 To control for which of the 301/302/303 status codes libcurl should *not* 97 switch back to GET for when doing a custom POST, and instead keep the custom 98 method, use CURLOPT_POSTREDIR(3). 99 100 If you prefer a custom POST method to be reset to exactly the method `POST`, 101 use CURLFOLLOW_FIRSTONLY instead. 102 103 ## CURLFOLLOW_FIRSTONLY (3) 104 105 When there is a custom request method set with CURLOPT_CUSTOMREQUEST(3), that 106 set method replaces what libcurl would otherwise use in the first outgoing 107 request only. The second request is then done according to the redirect 108 response code. 109 110 If you prefer your custom method to remain in use after a 307/308 redirect, 111 use CURLFOLLOW_OBEYCODE instead. 112 113 ## 114 115 # NOTE 116 117 Since libcurl changes method or not based on the specific HTTP response code, 118 setting CURLOPT_CUSTOMREQUEST(3) while following redirects may change what 119 libcurl would otherwise do and if not that carefully may even make it 120 misbehave since CURLOPT_CUSTOMREQUEST(3) overrides the method libcurl would 121 otherwise select internally. 122 123 Setting the CURLFOLLOW_OBEYCODE bit makes libcurl *not* use the custom set 124 method after redirects for 301, 302 and 303 responses. Unless the 125 CURLOPT_POSTREDIR(3) bits are set for those status codes. 126 127 # DEFAULT 128 129 0, disabled 130 131 # %PROTOCOLS% 132 133 # EXAMPLE 134 135 ~~~c 136 int main(void) 137 { 138 CURL *curl = curl_easy_init(); 139 if(curl) { 140 curl_easy_setopt(curl, CURLOPT_URL, "https://example.com"); 141 142 /* example.com is redirected, so we tell libcurl to follow redirection */ 143 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); 144 145 curl_easy_perform(curl); 146 } 147 } 148 ~~~ 149 150 # %AVAILABILITY% 151 152 # RETURN VALUE 153 154 curl_easy_setopt(3) returns a CURLcode indicating success or error. 155 156 CURLE_OK (0) means everything was OK, non-zero means an error occurred, see 157 libcurl-errors(3).