cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: how to Post data WITHOUT form?

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

2007/11/1, Tigersoft Development Group <tigersoft.dg_at_gmail.com>:
> 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:

(pseudo-code)

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);
    ret.allocsz=1024;
    ret.sz=0;

    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);
    curl_easy_perform(h);
    curl_easy_cleanup(h);

    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