cURL / Mailing Lists / curl-library / Single Mail

curl-library

(CURLOPT_WRITEFUNCTION + curl_formadd) problem on MSVS 2005

From: Anthony K <tony-k_at_mail.ru>
Date: Wed, 14 May 2008 00:20:03 +0700

/*

Hi.
I have problem with libcurl, at win32 system with curl_formadd and CURLOPT_WRITEFUNCTION.

It's modified C code example.
If curl_formadd functions not commented "mycallback" function doesn't executes.
I got this problem on win32 system with MSVS 2005, on Linux/FreeBSD with GCC this code works without any problems.

Thnx for help!
Tony.

*/

#include <stdio.h>
#include <string.h>

#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>

size_t mycallback(void * buffer, size_t n, size_t m, void * stream)
{
        char * l = (char*)buffer;
        printf("data: %s \n", l);
        return n;
}

int main(int argc, char *argv[])
{
  CURL *curl;
  CURLcode res;

  struct curl_httppost *formpost=NULL;
  struct curl_httppost *lastptr=NULL;
  struct curl_slist *headerlist=NULL;
  char buf[] = "Expect:";

  curl_global_init(CURL_GLOBAL_ALL);
  
  //curl = curl_easy_init();

  curl_formadd(&formpost,
               &lastptr,
               CURLFORM_COPYNAME, "sendfile",
               CURLFORM_FILE, "postit2.c",
               CURLFORM_END);

  curl_formadd(&formpost,
               &lastptr,
               CURLFORM_COPYNAME, "filename",
               CURLFORM_COPYCONTENTS, "postit2.c",
               CURLFORM_END);

  curl_formadd(&formpost,
               &lastptr,
               CURLFORM_COPYNAME, "submit",
               CURLFORM_COPYCONTENTS, "send",
               CURLFORM_END);

  curl = curl_easy_init();
  /* initalize custom header list (stating that Expect: 100-continue is not
     wanted */
  headerlist = curl_slist_append(headerlist, buf);
  if(curl) {
    /* what URL that receives this POST */
    curl_easy_setopt(curl, CURLOPT_URL, "http://curl.haxx.se/examplepost.cgi");
    if ( (argc == 2) && (!strcmp(argv[1], "noexpectheader")) )
      /* only disable 100-continue header if explicitly requested */
      curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
    curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, NULL);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, mycallback);
        
    res = curl_easy_perform(curl);

    /* always cleanup */
    curl_easy_cleanup(curl);

    /* then cleanup the formpost chain */
    curl_formfree(formpost);
    /* free slist */
    curl_slist_free_all (headerlist);
  }
  return 0;
}
Received on 2008-05-13