curl-library
Re: How to pass custom data on top of HTTP GET
Date: Wed, 29 Dec 2010 18:46:15 -0500
On Tue, Dec 28, 2010 at 5:08 PM, Daniel Stenberg <daniel_at_haxx.se> wrote:
> On Tue, 28 Dec 2010, amit paliwal wrote:
>
> I want to pass additional custom data on top of HTTP GET to the HTTP
>> Server, which means I need to build HTTP GET message including the payload
>> and send it to HTTP Server by saying curl_easy_perform().
>>
>> Do I need to use CURLOPT_READFUNCTION option and maintain one callback
>> function where i can append my custom data? But then how will the library
>> call my custom payload and send it on top of HTTP GET.
>>
>
> Make the request out to be a POST, like use CURLOPT_POSTFIELDS or
> CURLOPT_READFUNCTION or whatever you think is the best way.
>
> Then you alter the method to a GET with CURLOPT_CUSTOMREQUEST and you
> should be good to go. You may want to remove/alter the "Content-Type:" that
> a mere POST will make libcurl use by default (use CURLOPT_HTTPHEADER for
> that).
>
> -- Reply: I tried the above step but it did not work. I received HTTP
> response message from server and my callback got called with the payload. In
> my callback I forced to send another GET message and tried the gET to take
> data from my read callback, but it did nto happen that way.
>
sample code goes like this:
size_t write_data( void *buffer, size_t size, size_t nmemb, void *userp )
{
int segsize = size * nmemb;
/*TODO: we can think of making it a thread specific attribute as it will
be required for
* individual session*/
CURL *ctx = (CURL*)userp;
printf("\nCurl object address is:%p",ctx);
CURLcode ret;
/*Prepare HTTP GET message here to send the response of the received DAPro
Message*/
/* We need to pass the easy handle as User data to the write callback
*/
curl_easy_setopt( ctx , CURLOPT_READDATA ,ctx);
/* Set DAPro Parser's static function as write callback.
*/
curl_easy_setopt( ctx , CURLOPT_READFUNCTION , readcb );
/* Set the Request message to be a HTTP GET method
*/
curl_easy_setopt(ctx, CURLOPT_CUSTOMREQUEST, "GET") ;
ret = curl_easy_perform(ctx);
if(CURLE_OK != ret)
{
printf("Error: %s\n", strerror(ret));
}
/* Return the number of bytes received, indicating to curl that all is
okay */
return segsize;
}
size_t readcb(void *ptr, size_t size, size_t nitems, void *stream)
{
printf("\nEntered inside read callback");
}
When write_data is executed and I send HTTP GET, I expect it to go in
readcb() function to take my custom data, but it is not happening.
> / daniel.haxx.se
> -------------------------------------------------------------------
> List admin: http://cool.haxx.se/list/listinfo/curl-library
> Etiquette: http://curl.haxx.se/mail/etiquette.html
>
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2010-12-30