Skip to content

--follow doesn't respect 303 method change unless --data also present #20715

Closed

Description

@thedanbob

I did this

curl --request <VERB> --follow http://some/url/that/returns/303

I expected the following

Curl to follow the redirect using GET. Instead, the redirect is followed with the original method.

The redirect is correctly changed to GET if curl is called with empty --data:

curl --request <VERB> --data '' --follow http://some/url/that/returns/303

but I wouldn't expect that to be necessary and it's not documented.

The issue presents with all HTTP verbs, including the ones that don't have dedicated flags (e.g. DELETE).

curl/libcurl version

curl 8.18.0 (x86_64-pc-linux-gnu) libcurl/8.18.0 OpenSSL/3.6.1 zlib/1.3.2 brotli/1.2.0 zstd/1.5.7 libidn2/2.3.8 libpsl/0.21.5 libssh2/1.11.1 nghttp2/1.68.0 ngtcp2/1.20.0 nghttp3/1.15.0 mit-krb5/1.21.3
Release-Date: 2026-01-07
Protocols: dict file ftp ftps gopher gophers http https imap imaps ipfs ipns mqtt pop3 pop3s rtsp scp sftp smb smbs smtp smtps telnet tftp ws wss
Features: alt-svc AsynchDNS brotli GSS-API HSTS HTTP2 HTTP3 HTTPS-proxy IDN IPv6 Kerberos Largefile libz NTLM PSL SPNEGO SSL threadsafe TLS-SRP UnixSockets zstd

operating system

Linux linux-dra 6.12.74-1-lts #1 SMP PREEMPT_DYNAMIC Thu, 19 Feb 2026 16:48:19 +0000 x86_64 GNU/Linux

(Arch linux)

Activity

  1. self-assigned this
    on Feb 24, 2026
  2. dfandrich commented on Feb 25, 2026

    @dfandrich
    Contributor
  3. thedanbob commented on Feb 25, 2026

    @thedanbob
    Author

    True, that's the case for curl --request POST --data '' etc, but the same issue is present for the other HTTP verbs. I would think that if --data was required to make --follow work correctly it would at least be mentioned in the docs, but the example given in the man entry is

    Example:
    curl -X POST --follow https://example.com
    

    Edit: Also, as far as I can tell the only reason for --follow is to handle invocations with --request. E.g. curl --data '' --location http://returns/303 also switches to GET for the redirect.

  4. added a commit that references this issue on Feb 25, 2026
    cb3ac6e
  5. dfandrich commented on Feb 25, 2026

    @dfandrich
    Contributor
  6. dfandrich commented on Feb 25, 2026

    @dfandrich
    Contributor
  7. thedanbob commented on Feb 25, 2026

    @thedanbob
    Author

    That’s fair, but the documentation also says

    If --location is used, the method string you set with --request is used for all requests, which may cause unintended side-effects when curl does not change request method according to the HTTP 30x response codes - and similar. Consider using --follow instead in combination with --request.

    As far as I know the only way to get e.g. DELETE with curl is by using --request. It isn’t obvious from the docs that you need to add --data to the DELETE (uncommon) to get --follow to work. It just says “use --follow with --request”.

  8. thedanbob commented on Feb 25, 2026

    @thedanbob
    Author

    I've edited the original issue to make it clear that it's a problem with all verbs, including the ones without dedicated command line flags.

  9. vog commented on Feb 26, 2026

    @vog

    curl --request POST --follow http://some/url/that/returns/303
    Try adding the -v option. You'll see the following warning:

    Note: Unnecessary use of -X or --request, POST is already inferred.

    Then try again after removing --request. It is very seldom the right option to use.

    I propose to raise the level of this warning, so it doesn't need -v for it to be shown. This may safe lots of users from serious headaches.

  10. benslimanh commented on Feb 26, 2026

    @benslimanh
    Contributor

    Agreed the man page example is misleading.

  11. added a commit that references this issue on Feb 26, 2026
    1b3999f
  12. dfandrich commented on Feb 26, 2026

    @dfandrich
    Contributor
  13. jay commented on Mar 4, 2026

    @jay
    Member

    This seems like a bug or an oversight to me. I tried builds from 13cbabf (curl: add --follow) and master, using --request and --follow, with 301, 302 and 303 responses and all of them kept the original --request method.

  14. jay commented on Mar 4, 2026

    @jay
    Member

    Digging in a little deeper, --follow maps internally to CURLFOLLOW_OBEYCODE.

    --follow says (emphasis mine):

    The method string set with --request is used in subsequent requests for the status codes 307 or 308, but may be reset to GET for 301, 302 and 303.

    CURLFOLLOW_OBEYCODE says:

    Note that only POST (or a custom post) is changed to GET on 301/302, its not change PUT etc - and therefore also not when libcurl issues a custom PUT. A 303 response makes it switch to GET independently of the original method (except for HEAD).

    ... and then in the NOTE section it says:

    Setting the CURLFOLLOW_OBEYCODE bit makes libcurl not use the custom set method after redirects for 301, 302 and 303 responses. Unless the CURLOPT_POSTREDIR bits are set for those status codes.

    IMO the situation is confusing.

    The code in the http handler for 301/302/303 is very POST-specific, it's written in a way that it won't call http_switch_to_get for a just a custom request with obeycode:

    curl/lib/http.c

    Lines 1396 to 1461 in 2eebc58

    case 301: /* Moved Permanently */
    /* (quote from RFC7231, section 6.4.2)
    *
    * Note: For historical reasons, a user agent MAY change the request
    * method from POST to GET for the subsequent request. If this
    * behavior is undesired, the 307 (Temporary Redirect) status code
    * can be used instead.
    *
    * ----
    *
    * Many webservers expect this, so these servers often answers to a POST
    * request with an error page. To be sure that libcurl gets the page that
    * most user agents would get, libcurl has to force GET.
    *
    * This behavior is forbidden by RFC1945 and the obsolete RFC2616, and
    * can be overridden with CURLOPT_POSTREDIR.
    */
    if((data->state.httpreq == HTTPREQ_POST ||
    data->state.httpreq == HTTPREQ_POST_FORM ||
    data->state.httpreq == HTTPREQ_POST_MIME) &&
    !(data->set.keep_post & CURL_REDIR_POST_301)) {
    http_switch_to_get(data, 301);
    switch_to_get = TRUE;
    }
    break;
    case 302: /* Found */
    /* (quote from RFC7231, section 6.4.3)
    *
    * Note: For historical reasons, a user agent MAY change the request
    * method from POST to GET for the subsequent request. If this
    * behavior is undesired, the 307 (Temporary Redirect) status code
    * can be used instead.
    *
    * ----
    *
    * Many webservers expect this, so these servers often answers to a POST
    * request with an error page. To be sure that libcurl gets the page that
    * most user agents would get, libcurl has to force GET.
    *
    * This behavior is forbidden by RFC1945 and the obsolete RFC2616, and
    * can be overridden with CURLOPT_POSTREDIR.
    */
    if((data->state.httpreq == HTTPREQ_POST ||
    data->state.httpreq == HTTPREQ_POST_FORM ||
    data->state.httpreq == HTTPREQ_POST_MIME) &&
    !(data->set.keep_post & CURL_REDIR_POST_302)) {
    http_switch_to_get(data, 302);
    switch_to_get = TRUE;
    }
    break;
    case 303: /* See Other */
    /* 'See Other' location is not the resource but a substitute for the
    * resource. In this case we switch the method to GET/HEAD, unless the
    * method is POST and the user specified to keep it as POST.
    * https://github.com/curl/curl/issues/5237#issuecomment-614641049
    */
    if(data->state.httpreq != HTTPREQ_GET &&
    ((data->state.httpreq != HTTPREQ_POST &&
    data->state.httpreq != HTTPREQ_POST_FORM &&
    data->state.httpreq != HTTPREQ_POST_MIME) ||
    !(data->set.keep_post & CURL_REDIR_POST_303))) {
    http_switch_to_get(data, 303);
    switch_to_get = TRUE;
    }
    break;

    I could modify it of course to "fix" the reporter's issue (click here for diff)
    diff --git a/lib/http.c b/lib/http.c
    index 9de27ba..16a1ae0 100644
    --- a/lib/http.c
    +++ b/lib/http.c
    @@ -1321,10 +1321,12 @@ CURLcode Curl_http_follow(struct Curl_easy *data, const char *newurl,
          * This behavior is forbidden by RFC1945 and the obsolete RFC2616, and
          * can be overridden with CURLOPT_POSTREDIR.
          */
    -    if((data->state.httpreq == HTTPREQ_POST ||
    -        data->state.httpreq == HTTPREQ_POST_FORM ||
    -        data->state.httpreq == HTTPREQ_POST_MIME) &&
    -       !data->set.post301) {
    +    if(((data->state.httpreq == HTTPREQ_POST ||
    +         data->state.httpreq == HTTPREQ_POST_FORM ||
    +         data->state.httpreq == HTTPREQ_POST_MIME) &&
    +        !data->set.post301) ||
    +       (data->set.str[STRING_CUSTOMREQUEST] &&
    +        data->set.http_follow_mode == CURLFOLLOW_OBEYCODE)) {
           http_switch_to_get(data, 301);
           switch_to_get = TRUE;
         }
    @@ -1346,10 +1348,12 @@ CURLcode Curl_http_follow(struct Curl_easy *data, const char *newurl,
          * This behavior is forbidden by RFC1945 and the obsolete RFC2616, and
          * can be overridden with CURLOPT_POSTREDIR.
          */
    -    if((data->state.httpreq == HTTPREQ_POST ||
    -        data->state.httpreq == HTTPREQ_POST_FORM ||
    -        data->state.httpreq == HTTPREQ_POST_MIME) &&
    -       !data->set.post302) {
    +    if(((data->state.httpreq == HTTPREQ_POST ||
    +         data->state.httpreq == HTTPREQ_POST_FORM ||
    +         data->state.httpreq == HTTPREQ_POST_MIME) &&
    +         !data->set.post302) ||
    +       (data->set.str[STRING_CUSTOMREQUEST] &&
    +        data->set.http_follow_mode == CURLFOLLOW_OBEYCODE)) {
           http_switch_to_get(data, 302);
           switch_to_get = TRUE;
         }
    @@ -1361,11 +1365,13 @@ CURLcode Curl_http_follow(struct Curl_easy *data, const char *newurl,
          * method is POST and the user specified to keep it as POST.
          * https://github.com/curl/curl/issues/5237#issuecomment-614641049
          */
    -    if(data->state.httpreq != HTTPREQ_GET &&
    -       ((data->state.httpreq != HTTPREQ_POST &&
    -         data->state.httpreq != HTTPREQ_POST_FORM &&
    -         data->state.httpreq != HTTPREQ_POST_MIME) ||
    -        !data->set.post303)) {
    +    if((data->state.httpreq != HTTPREQ_GET &&
    +        ((data->state.httpreq != HTTPREQ_POST &&
    +          data->state.httpreq != HTTPREQ_POST_FORM &&
    +          data->state.httpreq != HTTPREQ_POST_MIME) ||
    +         !data->set.post303)) ||
    +       (data->set.str[STRING_CUSTOMREQUEST] &&
    +        data->set.http_follow_mode == CURLFOLLOW_OBEYCODE)) {
           http_switch_to_get(data, 303);
           switch_to_get = TRUE;
         }

    ... but I think proposing a change like that is wrong and would break test1573, among others maybe, because it seems the intention was for OBEYCODE to only change custom requests back to GET in certain circumstances.

    curl/tests/data/test1573

    Lines 68 to 88 in 2eebc58

    <name>
    CURLFOLLOW_OBEYCODE with custom GET method, 301 => custom
    </name>
    <command>
    http://%HOSTIP:%HTTPPORT/%TESTNUMBER
    </command>
    </client>
    # Verify data after the test has been "shot"
    <verify>
    <protocol crlf="yes">
    IGLOO /%TESTNUMBER HTTP/1.1
    Host: %HOSTIP:%HTTPPORT
    Accept: */*
    IGLOO /%TESTNUMBER0001 HTTP/1.1
    Host: %HOSTIP:%HTTPPORT
    Accept: */*
    </protocol>
    </verify>

    IMO it makes more sense to always ignore custom request on 301/302/303 if follow is on but I guess it's a little late for that.

  15. added 2 commits that reference this issue on Mar 26, 2026
    74332a0
    030e682
  16. added a commit that references this issue on Apr 9, 2026
    3f3d144
  17. added a commit that references this issue on Apr 10, 2026
    e5087ac
  18. added a commit that references this issue on Jun 3, 2026
    ca87ec0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions