Simran
Simran

Reputation: 133

HTTP Header not set using Curl in C++

I'm very new to HTTP commands and the libcurl library, so there is a good chance that I'm not understanding something fundamental. That said, I am trying to set headers using curl in C++ but when I try to get the Content-Type, a null value is returned. I am not able to figure out why this is happening. Here is the piece of code:

// HTTP headers to send with request
   struct curl_slist* headers = nullptr;

// Set Content-Type to application/x-hybrid-thrift-binary
   headers = curl_slist_append(headers, "Content-Type: application/x-hybrid-thrift-binary");

   curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
   CURLcode ret = curl_easy_perform(curl);

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

Any help would be highly appreciated!!!

Upvotes: 1

Views: 1753

Answers (1)

JensG
JensG

Reputation: 13411

From the libcurl docs:

SYNOPSIS

#include <curl/curl.h> 

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

DESCRIPTION

Pass a pointer to a char pointer to receive the content-type of the downloaded object. This is the value read from the Content-Type: field. If you get NULL, it means that the server didn't send a valid Content-Type header or that the protocol used doesn't support this.

RETURN VALUE

Returns CURLE_OK if the option is supported, and CURLE_UNKNOWN_OPTION if not.

Upvotes: 2

Related Questions