cURL / Mailing Lists / curl-library / Single Mail

curl-library

File transfer using http post

From: <madhusudhan.nanjappa_at_wipro.com>
Date: Fri, 2 Mar 2007 18:13:32 +0530

Hi,

I am in need of transferring a file to a server using http post. Being new to both http programming and libcurl is a deadly combination I guess. from the sample code available on the libcurl site here I was able to write this code.

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

size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
{
   size_t retcode;

   retcode = fread(ptr, size, nmemb, stream);

   fprintf(stderr, "*** We read %d bytes from file\n", retcode);

   return retcode;
}

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

   if(argc < 3)
   return 1;

   file= argv[1];
   url = argv[2];
   hd = open(file, O_RDONLY) ;
   fstat(hd, &file_info);
   close(hd) ;

   hd_src = fopen(file, "rb");

   curl_global_init(CURL_GLOBAL_ALL);

   curl = curl_easy_init();
   if(curl) {
   // curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
   // curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
      curl_easy_setopt(curl, CURLOPT_POST, 1);
      curl_easy_setopt(curl, CURLOPT_URL, url);
      curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
      curl_easy_setopt(curl, CURLOPT_INFILESIZE,(curl_off_t)file_info.st_size);
      printf("size:%d\n",file_info.st_size);

      res = curl_easy_perform(curl);
      printf("%d\n",res);
      curl_easy_cleanup(curl);
   }
   fclose(hd_src);
   curl_global_cleanup();
   return 0;
}

Ethereal packet analysis shows content-length to be zero. Can some one help me out?

Thanks for your time:)

Regards,
Madhu

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 2007-03-02