cURL / Mailing Lists / curl-users / Single Mail

curl-users

how to get the HTTP HEADERS along with the HTTP Content.

From: Venkataragavan Vijayakumar, TLS-Chennai <venkataragavan.v_at_hcl.in>
Date: Mon, 4 Feb 2008 17:48:11 +0530

Hi all,

            

            The following is the code I returned to get the HTTP Header
and the HTTP content.

 

But the problem is,

 

1. If I set callback functions to get header, using
CURLOPT_HEADERFUNCTION and to get content using CURLOPT_WRITEFUNCTION,
only one callback function is getting executed.

 

2. If I set CURLOPT_HEADER as 1, then only the header part
(HTTP/1.1 200 OK) is coming in the CURLOPT_WRITEFUNCTION registered
callback.

 

So, please anyone see my following code and help me to get the HTTP
HEADER and also the CONTENT

 

 

Get()

{

            CURL *curl;

            CURLcode res;

            char* data=NULL,*data1=NULL;

            char* match = "Etag:";

            curl = curl_easy_init();

            

            if(curl) {

                        

                        curl_easy_setopt(curl,
CURLOPT_URL,"xcap.example.com ");

 

                        curl_easy_setopt(curl,
CURLOPT_FOLLOWLOCATION,1);

                        

                        curl_easy_setopt(curl, CURLOPT_HTTPGET,1);

                        

                        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
read_callback);

                        

                        curl_easy_setopt(curl, CURLOPT_WRITEDATA,
&data);

                        

                        curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);

 

                        curl_easy_setopt(curl, CURLOPT_HEADER,1);

 

                        curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION,
get_xcap_etag);

                        

                        curl_easy_setopt(curl, CURLOPT_HEADERDATA,
&data1);

            

                        res = curl_easy_perform(curl);

            }

 

}

 

Callback functions.

 

 

size_t get_xcap_etag( void *ptr, size_t size, size_t nmemb, void
**userp)

{

 if(size*nmemb < 1)

            return 0;

 

  if (ptr)

  {

              *userp = malloc (strlen((char *)ptr) + 1);

              strcpy (*userp, (char *)ptr);

             

              return 1;

  }

  

  return -1;

}

 

 

static size_t read_callback(void *ptr, size_t size, size_t nmemb, void
**userp)

{

            

  if(size*nmemb < 1)

    return 0;

 

  if (ptr)

  {

              *userp = malloc (strlen((char *)ptr) + 1);

              strcpy (*userp, (char *)ptr);

              return 1;

  }

 

  return -1;

}

 

 

 

Thanks & Regards,

Venkataragavan V

 

 

 

DISCLAIMER:
-----------------------------------------------------------------------------------------------------------------------

The contents of this e-mail and any attachment(s) are confidential and intended for the named recipient(s) only.
It shall not attach any liability on the originator or HCL or its affiliates. Any views or opinions presented in
this email are solely those of the author and may not necessarily reflect the opinions of HCL or its affiliates.
Any form of reproduction, dissemination, copying, disclosure, modification, distribution and / or publication of
this message without the prior written consent of the author of this e-mail is strictly prohibited. If you have
received this email in error please delete it and notify the sender immediately. Before opening any mail and
attachments please check them for viruses and defect.

-----------------------------------------------------------------------------------------------------------------------
Received on 2008-02-04