GNURLOPT_POSTREDIR(3)

πŸ“¦ version 7.72.0, in nixos 26.05, nixpkgs unstable

πŸ’… GNURLOPT_POSTREDIR(3) β€” okay so this is curl_easy_setopt's options page, bestie πŸŽ€

Like, the manual name is literally curl_easy_setopt options, and this one specific option is the POST-redirect vibe check. Let's get into it ✨


🌸 NAME 🌸

CURLOPT_POSTREDIR β€” how to act on an HTTP POST redirect πŸ¦‹


✨ SYNOPSIS ✨

#include <gnurl/curl.h>

CURLcode curl_easy_setopt(CURL *handle, CURLOPT_POSTREDIR,
                          long bitmask);

πŸ’– DESCRIPTION πŸ’–

So you pass in a bitmask, right? And that bitmask controls how libcurl acts on redirects after POSTs that get a 301, 302 or 303 response back. No cap, it's that simple 🫢

  • A parameter with bit 0 set (value CURL_REDIR_POST_301) tells the library to respect RFC 7231 (section 6.4.2 to 6.4.4) and not convert POST requests into GET requests when following a 301 redirection. Respecting the RFC? We love to see it πŸ‘‘
  • Setting bit 1 (value CURL_REDIR_POST_302) makes libcurl maintain the request method after a 302 redirect. Iconic behaviour, honestly 🌈
  • Setting bit 2 (value CURL_REDIR_POST_303) makes libcurl maintain the request method after a 303 redirect. Slay πŸ’…

And the value CURL_REDIR_POST_ALL is a convenience define that sets all three bits β€” literally all the bits, one define, so convenient πŸ“

Also, the non-RFC behaviour is ubiquitous in web browsers, so the library does the conversion by default to maintain consistency. That's just how it is, bestie 😭 But a server may require a POST to remain a POST after such a redirection β€” and that's exactly when you'd want this.

Heads up: this option is meaningful only when setting CURLOPT_FOLLOWLOCATION(3). Like, it only does anything in that context, fr fr.

🌟 DEFAULT 🌟

0


🧚 PROTOCOLS 🧚

HTTP(S)


πŸ”₯ EXAMPLE πŸ”₯

CURL *curl = curl_easy_init();
if(curl) {
  curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");

  /* a silly POST example */
  curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "data=true");

  /* example.com is redirected, so we tell libcurl to send POST on 301, 302 and
     303 HTTP response codes */
  curl_easy_setopt(curl, CURLOPT_POSTREDIR, CURL_REDIR_POST_ALL);

  curl_easy_perform(curl);
}

πŸ¦‹ AVAILABILITY πŸ¦‹

Added in 7.17.1. And get this β€” this option was known as CURLOPT_POST301 up to 7.19.0, because it only supported the 301 then. Glow-up era, honestly ⭐ Also, CURL_REDIR_POST_303 was added in 7.26.0. She grew 🌱


πŸ’œ RETURN VALUE πŸ’œ

Returns CURLE_OK if the option is supported, and CURLE_UNKNOWN_OPTION if not. Simple as that πŸ™ˆ


πŸ‘‘ SEE ALSO πŸ‘‘


Okay that's the whole page, you ate that πŸ’…βœ¨ Signing off for libcurl 7.37.0, dated 19 Jun 2014, under the GNURLOPT_POSTREDIR(3) banner β€” bye bestie, go keep your POSTs as POSTs πŸŽ€πŸ«ΆπŸ’–

πŸ“¦ where this page comes from