curl-library
Custom http headers don't work
Date: Mon, 12 Jan 2009 20:48:29 +0000 (UTC)
I am trying to get custom headers into a GET request, but they aren't going
through (I verified this by turning VERBOSE on). I use a similar mechanism to
set custom headers on POST requests, and that seems to work just fine.
Here's a snippet of the code:
// Create CURL objects
CURL* pCurl;
CURLcode result;
// Initialize our handle
pCurl = curl_easy_init();
if( !pCurl )
{
MV_THROW( RESTClientException, "Unable to initialize CURL object!" );
}
// we will use this to check the result
long httpCode;
// set the url of the handle
curl_easy_setopt( pCurl, CURLOPT_URL, i_rURI.c_str() );
// set request headers
struct curl_slist* requestHeader = NULL;
std::map< std::string, std::string >::const_iterator iter =
i_rRequestHeaders.begin();
for( ; iter != i_rRequestHeaders.end(); ++iter )
{
curl_slist_append( requestHeader, (iter->first + ": " +
iter->second).c_str() );
}
curl_easy_setopt( pCurl, CURLOPT_HTTPHEADER, requestHeader );
// set compression type
if( i_CompressionType == GZIP )
{
curl_easy_setopt( pCurl, CURLOPT_ENCODING, "gzip" );
}
else if( i_CompressionType == DEFLATE )
{
curl_easy_setopt( pCurl, CURLOPT_ENCODING, "deflate" );
}
else if( i_CompressionType == ANY )
{
curl_easy_setopt( pCurl, CURLOPT_ENCODING, "gzip,deflate" );
}
// set the data buffer and write function to use for the result
curl_easy_setopt( pCurl, CURLOPT_WRITEDATA, &o_rData );
T_WriterType()( pCurl );
char errorBuffer[CURL_ERROR_SIZE];
errorBuffer[0] = '\0';
curl_easy_setopt( pCurl, CURLOPT_ERRORBUFFER, errorBuffer );
// ...so we can store all returned header information
std::string responseHeader;
curl_easy_setopt( pCurl, CURLOPT_HEADERFUNCTION, StringWriter );
curl_easy_setopt( pCurl, CURLOPT_WRITEHEADER, &responseHeader );
// perform the GET request!
curl_easy_setopt( pCurl, CURLOPT_VERBOSE, 1L );
result = curl_easy_perform( pCurl );
Anyone have the same problem, or notice anything about the code above
that's wrong?
Thanks in advance,
-Scott
Received on 2009-01-12