curl / Mailing Lists / curl-library / Single Mail

curl-library

Re: curl_formadd and empty content?

From: Patrick Monnerat via curl-library <curl-library_at_cool.haxx.se>
Date: Sat, 27 Jan 2018 04:57:58 +0100

On 01/26/2018 01:37 PM, Christian Schmitz wrote:
> Does someone know how to pass empty text to curl_formadd for content of a field?
>
> As far as I see, empty fields are skipped.
>
> So passing "x" as content, shows field with x.
> But passing "" as content, skips field.
What do you mean by "skipped"? I've just tested your case and the sent
form is OK:

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

gives:

POST /test.php HTTP/1.1
Host: localhost
Accept: */*
Content-Length: 141
Content-Type: multipart/form-data;
boundary=------------------------81cc2462e640e433

--------------------------81cc2462e640e433
Content-Disposition: form-data; name="submit"

--------------------------81cc2462e640e433--

If "skipped" means not present in server, maybe you should check what is
transmitted with wireshark and be sure it is not stripped on the server
side.

If you confirm your problem is on the client side, please post details
about it and a minimal program that makes it happen.

> I see that code uses strlen() and I see in formdata.c this:
>
> if(!clen)
> clen = -1;
>
> So if clen = 0, than it sets to -1, but curl_mime_data than does use strlen again.
>
>

Yes, that's normal: by convention, curl_formadd() uses a zero length as
a nul-terminated string indicator. From the curl_formadd.3
(https://curl.haxx.se/libcurl/c/curl_formadd.html) man page:

CURLFORM_CONTENTLEN
               followed by a curl_off_t value giving the  length  of
the  con‐
               tents.  Note  that  for CURLFORM_STREAM contents, this
option is
               mandatory.

               If you pass a 0 (zero) for this option, libcurl will
instead  do
               a strlen() on the contents to figure out the size. If you
really
               want to send a  zero  byte  content  then  you  must
make  sure
               strlen() on the data pointer returns zero.

curl_formadd() is deprecated and you should now use the mime api for new
designs: the mime function curl_mime_data() lets you give a real zero
length and has the special value CURL_ZERO_TERMINATED to use strlen() on
the data.
-------------------------------------------------------------------
Unsubscribe: https://cool.haxx.se/list/listinfo/curl-library
Etiquette: https://curl.haxx.se/mail/etiquette.html
Received on 2018-01-27