cURL / Mailing Lists / curl-library / Single Mail

curl-library

Passing char buffer pointer for PUT operation instead of file.

From: <tamal.biswas_at_wipro.com>
Date: Fri, 26 Dec 2008 14:33:00 +0530

 
Hi,
I still could not solve the problem of PUT operation.
What I want to do is, I want to implement my own call-back function and
pass a function pointer to it.

      curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
      curl_easy_setopt(curl, CURLOPT_READDATA,pData);
And want the program to read from this pData..

For clarity sake.. In the following program, I am not passing pData
also. Just taking some local string and want to pass the same. Can
anyone please help me to find out the prob, or I am missing something.
Remote file is just getting populated with some junk data.

This is what I have done:

 #include <stdio.h>
  #include <fcntl.h>
  #include <sys/stat.h>
  #include <unistd.h>
  #include <string.h>
  #include <curl/curl.h>
  #include <stdlib.h>

char buff[30];
char pData[3]={'T','A','M'};

int count=0;
  static size_t read_callback(void *ptr, size_t size, size_t nmemb, void
*stream)
  {
            size_t retcode=0;
        char buff1[10]="TAMAL TAN";
        ptr=buff1;//want to populate ptr using data from char buffer.
        if(count != 0)
        {
                printf("returning 0");
                return 0;
        }
        count++;
        size=1;
        nmemb=9;//NOT including NULL character.

        printf("\n************before**********\n");
        printf("size = %d\n",size);
        printf("nmemb = %d\n",nmemb);
        printf("stream= %p\n",stream);
        printf("ptr= %p\n",ptr);

        // retcode = fread(ptr, size, nmemb,(FILE *)stream);
        // Not using fread operation. wants to read it from buffer
itself.
        printf("\n***************after**********\n");
        printf("size = %d\n",size);
        printf("nmemb = %d\n",nmemb);
        printf("stream= %p\n",stream);
        printf("ptr= %p\n",ptr);

        return 9;

  }

  int main(int argc, char **argv)
  {
    CURL *curl;
    CURLcode res;
    FILE * hd_src ;
    int hd ;
    struct stat file_info;

    char *file;
    char *url;

    file="a.txt";
    url ="http://10.203.37.4/xcap-root/rls-services/users/prav/index1";
    hd_src = fopen(file, "rb");
    curl_global_init(CURL_GLOBAL_ALL);
    curl = curl_easy_init();
    if(curl) {
      curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
     //curl_easy_setopt(curl, CURLOPT_PUT, 1L);
      curl_easy_setopt(curl, CURLOPT_URL, url);
      /* we want to use our own read function */
      curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
      // will not pass any data.. for testing will be passing data from
the callback itself.
      //curl_easy_setopt(curl, CURLOPT_READDATA,pData);
      /* Now run off and do what you've been told! */
      res = curl_easy_perform(curl);
      /* always cleanup */
     curl_easy_cleanup(curl);
   }
   fclose(hd_src); /* close the local file */
   curl_global_cleanup();
   return 0;
}

The program is writing some garbage in the remote URI. Please suggest.

Thanks & Regards
Tamal Tanu Biswas

------------------------------

Message: 4
Date: Wed, 24 Dec 2008 08:17:30 -0800
From: Dan Fandrich <dan_at_coneharvesters.com>
Subject: Re: How to PUT data char buffer??
To: curl-library_at_cool.haxx.se
Message-ID: <20081224161729.GA20667_at_coneharvesters.com>
Content-Type: text/plain; charset=us-ascii

On Wed, Dec 24, 2008 at 05:47:09PM +0530, tamal.biswas_at_wipro.com wrote:
> Hi,
> I am able to pass function pointer to the callback function and do the

> PUT operation. but I actually need to pass char buffer. How can I do
> the same without using any file mechanism.

Take a look at the getinmemory.c example program and do something
similar, in reverse. Basically, you just keep a pointer around which
always points to the current location in the data to send.

>>> Dan

-- 
http://www.MoveAnnouncer.com              The web change of address
service
          Let webmasters know that your web site has moved
Please do not print this email unless it is absolutely necessary. 
The information contained in this electronic message and any attachments to this message are intended for the exclusive use of the addressee(s) and may contain proprietary, confidential or privileged information. If you are not the intended recipient, you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately and destroy all copies of this message and any attachments. 
WARNING: Computer viruses can be transmitted via email. The recipient should check this email and any attachments for the presence of viruses. The company accepts no liability for any damage caused by any virus transmitted by this email. 
www.wipro.com
Received on 2008-12-26