cURL / Mailing Lists / curl-library / Single Mail


Re: how to Post data WITHOUT form?

From: Emil Romanus <>
Date: Fri, 2 Nov 2007 14:52:44 +0100

2007/11/1, Tigersoft Development Group <>:
> Just get data from char *data and send it to server.
> After data is sent - I need to get a raw response as char *response.
> Any suggestions? ;/

I think you theoretically can do something like this, but I'm NOT sure
it's valid to post binary data like this:


struct sizedstr { char* s; unsigned int allocsz; unsigned int sz; };

/* write callback */
size_t wrcb( void *ptr, size_t size, size_t nmemb, struct sizedstr *dsa)
    /* see if dsa->sz+(size*nmemb) fits into dsa->allocsz, else
realloc dsa->s */
    /* set dsa->sz accordingly, and update dsa->allocsz if realloced */
    /* return size*nmemb */

char* funny(const char *data, unsigned int len)
    CURL* h = curl_easy_init();
    struct sizedstr ret;
    ret.s = malloc(1024);

    curl_easy_setopt(h, CURLOPT_URL, "http://server/dsa/");
    curl_easy_setopt(h, CURLOPT_POSTFIELDS, data);
    curl_easy_setopt(h, CURLOPT_POSTFIELDSIZE, len);
    curl_easy_setopt(h, CURLOPT_WRITEFUNCTION, &wrcb);
    curl_easy_setopt(h, CURLOPT_WRITEDATA, &ret);

    return ret.s;

So there you have a function doing exactly what you asked for. Don't
forget to free() what funny() returned. You're lucky I was bored
enough to write this for you, it's all available in example code files
and the documentation.

If it doesn't work, then you'll probably have to do a multipart post,
and give the binary data your uploading a name so that your
server-side script is able to properly fetch it. See example code
files for multipart posts.
Received on 2007-11-02