Teresa Thomas | 2 Jul 2006 08:44
Picon

POSTing data using curl

Hi,
I am a newbie to Unix and libcurl and need some help in using the library. I want to make an HTTP POST request with a header and content. The content is a plain text file which contains XML data.

I figure that I add the header using:
    struct curl_slist *headers=NULL;
    headers = curl_slist_append(headers, header);   //header is char* type

Now, how can I set the content to be the XML file
Do I have to use the curl_easy_setopt function? If so, what are the parameters?

Thanks in advance!

Teresa Thomas

Fredrik Gustafsson | 2 Jul 2006 10:42
Picon

Re: POSTing data using curl

Hello and welcome to the world of OSS and Unix :).
A god tip to know what to post is to use the firefox plugin "Live HTTP
Headers" that would log the headers that is sent. Just simple activate
the plugin and to the task with firefox. Look at the log and then
implement the same in your application.

Below I'll post some of testingcode for login in to a website. swap is a
file that store the result, the returned html code. 

        swap = fopen("/tmp/iveHelgon.net","w");
        curl_easy_reset(curl);
        string post_this = "action=signin&cwidth=956&height=625&sill=" +
nick + "&fisk=" + passwd;
        curl_easy_setopt(curl, CURLOPT_URL,
"http://www.helgon.net/signin.asp");
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_this.c_str());
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, swap);
        if(!(curl_easy_perform(curl) == 0))
        {
            return 1;
        }
        fclose(swap);

Good luck
/iveqy
On Sun, Jul 02, 2006 at 02:44:45AM -0400, Teresa Thomas wrote:
> Hi,
> I am a newbie to Unix and libcurl and need some help in using the library. I
> want to make an HTTP POST request with a header and content. The content is
> a plain text file which contains XML data.
> 
> I figure that I add the header using:
>    struct curl_slist *headers=NULL;
>    headers = curl_slist_append(headers, header);   //header is char* type
> 
> Now, how can I set the content to be the XML file?
> Do I have to use the curl_easy_setopt function? If so, what are the
> parameters?
> 
> Thanks in advance!
> 
> Teresa Thomas

Teresa Thomas | 2 Jul 2006 19:01
Picon

Re: curl-library Digest, Vol 11, Issue 2

Thanks Mr Gustafsson for your tip :) 

I could do that, but since I pretty much know what exactly to post, I was wondering if anybody knew the parameters I have to use in: 
curl_easy_setopt(curlHandle, XXX , filename? )
to make the content of the POST contain a plain text file (which contains XML data).

Its probably mentioned somewhere in the documentation and I missed it.

Teresa Thomas

PS: Because of the nature of my application, using the FireFox plugin does not really aid the purpose. But I'll keep what you said in mind :)

Teresa Thomas | 2 Jul 2006 19:35
Picon

Re: POSTing data using curl

Thanks Mr Gustafsson for your tip :) 

I could do that, but since I pretty much know what exactly to post, I was wondering if anybody knew the parameters I have to use in: 
curl_easy_setopt(curlHandle, XXX , filename? )
to make the content of the POST contain a plain text file (which contains XML data).

Its probably mentioned somewhere in the documentation and I missed it.

Teresa Thomas

PS: Because of the nature of my application, using the FireFox plugin does not really aid the purpose. But I'll keep what you said in mind :)
Nimrod A. Abing | 2 Jul 2006 20:09
Picon

Re: POSTing data using curl

On 7/2/06, Teresa Thomas <tere.ertw <at> gmail.com> wrote:
> Hi,
> I am a newbie to Unix and libcurl and need some help in using the library. I
> want to make an HTTP POST request with a header and content. The content is
> a plain text file which contains XML data.
>
> I figure that I add the header using:
>     struct curl_slist *headers=NULL;
>     headers = curl_slist_append(headers, header);   //header is char* type
>
> Now, how can I set the content to be the XML file?
>  Do I have to use the curl_easy_setopt function? If so, what are the
> parameters?

curl_easy_setopt does not seem to have any options to set the POST
content type. So I use something like this:

curl_httppost* post = NULL;
curl_httppost* last = NULL;
curl_slist* upstreamHeaders = NULL;

curl_formadd(&post, &last, CURLFORM_COPYNAME, "src_file",
CURLFORM_FILE, filenameStr_psz, CURLFORM_CONTENTTYPE, mimeType_psz,
CURLFORM_END);

In this case, I am setting the field name to "src_file" and letting
libcurl handle reading and POSTing the file found in filenameStr_psz
using the Content-Type found in mimeType_psz.

In your case, filenameStr_psz will contain the NULL-terminated string
"text/xml".

See: http://curl.haxx.se/libcurl/c/curl_formadd.html

Just make sure you clean up post and last structures using
curl_formfree after you have performed the POST.

HTH.
--

-- 
_nimrod_a_abing_

"The world is a tragedy to those who feel and a comedy to those who
think." - Shakespeare

Ingmar Runge | 3 Jul 2006 15:34
Picon

7.15.4 introduced crash problem

Hi everyone!

I have been using libcurl for a while now (it's excellent!) and wanted
to upgrade to 7.15.4. Unfortunately, it crashes on me. It's working fine
with the curl client binary. It's not in my application, neither with
the release nor with the CVS snapshot from today (both compiled with
MSVC 6.0).
I'm running Windows XP. Three screenshots providing further useful
information are attached. It seems to be a share and multi
interface-related problem from what I see. I removed the
curl_easy_setopt(CURLOPT_SHARE) call and everything was fine again.
Unfortunately I'm not deep enough into C and/or the cURL source code, so
I have to call for help here for now.
Any help would be appreciated.

Thanks!
Ingmar Runge

m. allan noah | 3 Jul 2006 16:06

Re: POSTing data using curl

On Mon, 3 Jul 2006, Nimrod A. Abing wrote:

> On 7/2/06, Teresa Thomas <tere.ertw <at> gmail.com> wrote:
>> Hi,
>> I am a newbie to Unix and libcurl and need some help in using the library. 
>> I
>> want to make an HTTP POST request with a header and content. The content is
>> a plain text file which contains XML data.
>>

there are two ways (that i know of) to get a file to the server via POST.

one is the way suggested by nimrod, using a multipart/formdata post, which 
is just like filling out a form with a file upload box using your browser.
this gives you the ability to transfer other data besides the file, and 
some additional data about the file itself, like its name and type.

the second way, is to use CURLOPT_POSTFIELDS for curl_easy_setopt, which 
basically tells curl not to interpret what you are sending, and gives you 
complete control over the post body. you will have to make sure your xml 
file is encoded in the fashion that your webserver wants, and make sure 
that you tell curl how long it is (after encoding) with 
CURLOPT_POSTFIELDSIZE. you will have to provide the data as a buffer, not 
a filename. you will also have to play with the headers to tell curl what 
the content type is, so it can tell the server...

read docs here:

http://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTPOSTFIELDS

allan

Teresa Thomas | 3 Jul 2006 21:36
Picon

Re: POSTing data using curl

Thanks Nimrod!

Libcurl sends the POST request when I set the headers and did the following:

     curl_formadd(&post, &last,
                        CURLFORM_COPYNAME, "src_file",
                        CURLFORM_FILE, "sample_entry.xml",
          CURLFORM_CONTENTTYPE, "application/atom+xml",
                            CURLFORM_END);

But in the body of the response I get the following message:
" Content-Type must be 'application/atom+xml', not 'multipart/form-data;boundary=----------------------------bc7efa48e2cf;charset=iso-8859-1'."

Any libcurl geniuses know how I can get around that?


Any suggestion is appreciated!

Just to mention the background here, I am trying to send this POST request to add an entry to the Google Calendar Service. Here is what they instructed:

"Before you send that (the POST) request, you have to set the Authorization header in the request, using the following format:

Authorization: GoogleLogin auth=yourAuthTokenThe content that you send with the POST request should be the <entry> element you created above, using the application/atom+xml content type."

On 7/2/06, Nimrod A. Abing <nimrod.abing <at> gmail.com> wrote:
On 7/2/06, Teresa Thomas <tere.ertw <at> gmail.com> wrote:
> Hi,
> I am a newbie to Unix and libcurl and need some help in using the library. I
> want to make an HTTP POST request with a header and content. The content is
> a plain text file which contains XML data.
>
> I figure that I add the header using:
>     struct curl_slist *headers=NULL;
>     headers = curl_slist_append(headers, header);   //header is char* type
>
> Now, how can I set the content to be the XML file?
>  Do I have to use the curl_easy_setopt function? If so, what are the
> parameters?

curl_easy_setopt does not seem to have any options to set the POST
content type. So I use something like this:

curl_httppost* post = NULL;
curl_httppost* last = NULL;
curl_slist* upstreamHeaders = NULL;

curl_formadd(&post, &last, CURLFORM_COPYNAME, "src_file",
CURLFORM_FILE, filenameStr_psz, CURLFORM_CONTENTTYPE, mimeType_psz,
CURLFORM_END);

In this case, I am setting the field name to "src_file" and letting
libcurl handle reading and POSTing the file found in filenameStr_psz
using the Content-Type found in mimeType_psz.

In your case, filenameStr_psz will contain the NULL-terminated string
"text/xml".

See: http://curl.haxx.se/libcurl/c/curl_formadd.html

Just make sure you clean up post and last structures using
curl_formfree after you have performed the POST.

HTH.
--
_nimrod_a_abing_

"The world is a tragedy to those who feel and a comedy to those who
think." - Shakespeare

Daniel Stenberg | 3 Jul 2006 23:11
Picon
Favicon
Gravatar

Re: yangtse: curl/lib hostip.h,1.48,1.49 hostip6.c,1.27,1.28

On Mon, 3 Jul 2006, cvs <at> labb.contactor.se wrote:

> -int curl_dogetnameinfo(const struct sockaddr *sa, socklen_t salen,
> -                       char *host, size_t hostlen,
> -                       char *serv, size_t servlen, int flags,
> +int curl_dogetnameinfo(GETNAMEINFO_QUAL_ARG1 GETNAMEINFO_TYPE_ARG1 sa,
> +                       GETNAMEINFO_TYPE_ARG2 salen,
> +                       char *host, GETNAMEINFO_TYPE_ARG46 hostlen,
> +                       char *serv, GETNAMEINFO_TYPE_ARG46 servlen,
> +                       GETNAMEINFO_TYPE_ARG7 flags,
>                        int line, const char *source)

One word of caution here:

Remember that we have a fair amount of platforms that don't run configure and 
thus you must somehow provide a sensible default for these in a header file 
for those systems...

--

-- 
  Commercial curl and libcurl Technical Support: http://haxx.se/curl.html

Daniel Stenberg | 3 Jul 2006 23:27
Picon
Favicon
Gravatar

Re: 7.15.4 introduced crash problem

On Mon, 3 Jul 2006, Ingmar Runge wrote:

> I have been using libcurl for a while now (it's excellent!) and wanted to 
> upgrade to 7.15.4. Unfortunately, it crashes on me. It's working fine with 
> the curl client binary. It's not in my application, neither with the release 
> nor with the CVS snapshot from today (both compiled with MSVC 6.0).

You mention upgrade, which version worked fine for you previously then?

> I'm running Windows XP. Three screenshots providing further useful
> information are attached. It seems to be a share and multi
> interface-related problem from what I see. I removed the
> curl_easy_setopt(CURLOPT_SHARE) call and everything was fine again.

Really? That's very surprising. That third screen dump shows a call that 
deletes a hash from an east handle when that is added to a multi handle, and 
that is done unconditionally for multi interface and should do the same no 
matter if anything is shared or not.

What exactly are you sharing between the handles?

Did any attempts in making a stand-alone app that can repeat this?

--

-- 
  Commercial curl and libcurl Technical Support: http://haxx.se/curl.html


Gmane