GNURLINFO_CONTENT_TYPE(3)

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

πŸ’… GNURLINFO_CONTENT_TYPE(3) πŸ’…

okay so bestie, this page lives in the curl_easy_getinfo options manual, and it’s here to explain how you get the Content-Type. no cap, it’s a whole vibe.


✨ NAME ✨

CURLINFO_CONTENT_TYPE β€” get Content-Type

πŸŽ€ SYNOPSIS πŸŽ€

include this header and call this:

#include <gnurl/curl.h>

CURLcode curl_easy_getinfo(CURL *handle, CURLINFO_CONTENT_TYPE, char
**ct);

πŸ¦‹ DESCRIPTION πŸ¦‹

okay so you pass a pointer to a char pointer, and it receives the content-type of the downloaded object. this is literally the value read from the Content-Type: field β€” slay.

if you get NULL, it means the server didn’t send a valid Content-Type header or the protocol used doesn’t support this.

the ct pointer will be NULL or pointing to private memory β€” you MUST NOT free it. it gets freed when you call curl_easy_cleanup(3) on the corresponding CURL handle. periodt. πŸ’…

🌈 PROTOCOLS 🌈

HTTP(S) β€” that’s the whole list, bestie.

πŸ”₯ EXAMPLE πŸ”₯

here’s the code vibe check:

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

  res = curl_easy_perform(curl);

  if(!res) {
    /* extract the content-type */
    char *ct = NULL;
    res = curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ct);
    if(!res && ct) {
      printf("Content-Type: %s\n", ct);
    }
  }
  curl_easy_cleanup(curl);
}

⭐ AVAILABILITY ⭐

added in 7.9.4, so it’s been iconic for a minute.

πŸ’– RETURN VALUE πŸ’–

returns CURLE_OK if the option is supported, and CURLE_UNKNOWN_OPTION if not. simple as that.

🫢 SEE ALSO 🫢

go peek at curl_easy_getinfo(3), curl_easy_setopt(3), (yes, the trailing comma is canon πŸ’€). iykyk.


xoxo, your Unix bestie πŸ’– β€” this page comes from libcurl 7.44.0, dated 1 Sep 2015, and it’s the GNURLINFO_CONTENT_TYPE(3) page. bye bestie πŸŽ€

πŸ“¦ where this page comes from