Discussion:
POST Request with non-null-terminated string
Milen Nikolov via curl-library
2021-04-23 15:02:07 UTC
Permalink
Hi everyone,

I'm using libcurl in C++ and I have to make a post request to a server, containing a string in the body section. Another -condition is that I have to specify the content-type as octet-stream. I've set the header, authentication and so on, but there is one problem: the curl function (CURLOPT_POSTFIELDS) takes as argument a c-string. In my string there are also NUL characters, which have to be sent. When I try sending the request the body is truncated at the first NUL chararcter. I've tried saving my string in a char array and then providing it as an argument, but this didn't solve the issue, although if I try to print out chars after the first NUL in the console, they print out fine. So my question is if there's a workaround, which will allow me to solve the issue or is it impossible due to the fact that curl is a C library and the null-terminated strings are the C standard?

Best Regards,
Milen
Daniel Stenberg via curl-library
2021-04-23 15:17:24 UTC
Permalink
Post by Milen Nikolov via curl-library
I'm using libcurl in C++ and I have to make a post request to a server,
containing a string in the body section. Another -condition is that I have
to specify the content-type as octet-stream. I've set the header,
authentication and so on, but there is one problem: the curl function
(CURLOPT_POSTFIELDS) takes as argument a c-string. In my string there are
also NUL characters, which have to be sent. When I try sending the request
the body is truncated at the first NUL chararcter.
Set CURLOPT_POSTFIELDSIZE to the size of the data and it will not do strlen()
to figure out the size.

I also just filed https://github.com/curl/curl/pull/6943 to clarify this
better.
--
/ daniel.haxx.se
| Commercial curl support up to 24x7 is available!
| Private help, bug fixes, support, ports, new features
| https://www.wolfssl.com/contact/
-------------------------------------------------------------------
Unsubscribe: https://cool.haxx.se/list/listinfo/curl-library
Etiquette: https://curl.se/ma
Milen Nikolov via curl-library
2021-04-26 16:44:48 UTC
Permalink
Hi,

thanks for the answer, I got a few more suggesting the same option. I tried it today and it worked fine. I have a follow up question though. In the string I'm sending there are extended ASCII characters. I'm currently sending them to a test server and what I'm getting back is incorrect. However since I haven't set up the server it's hard for me to know if the problem is with curl or with the way the server is set up, so I wanted to know if the extended ASCII chars in the POST field are getting sent correctly. I tried searching but I only found suggestions to URL encode them, which in my case is impossible, since the server is supposed to read the HEX value of the symbol using the Latin 1 encoding.

Best Regards,
Milen
Post by Daniel Stenberg via curl-library
Post by Milen Nikolov via curl-library
I'm using libcurl in C++ and I have to make a post request to a server,
containing a string in the body section. Another -condition is that I have
to specify the content-type as octet-stream. I've set the header,
authentication and so on, but there is one problem: the curl function
(CURLOPT_POSTFIELDS) takes as argument a c-string. In my string there are
also NUL characters, which have to be sent. When I try sending the request
the body is truncated at the first NUL chararcter.
Set CURLOPT_POSTFIELDSIZE to the size of the data and it will not do strlen()
to figure out the size.
I also just filed https://github.com/curl/curl/pull/6943 to clarify this
better.
--
/ daniel.haxx.se
| Commercial curl support up to 24x7 is available!
| Private help, bug fixes, support, ports, new features
| https://www.wolfssl.com/contact/
-------------------------------------------------------------------
Unsubscribe: https://cool.haxx.se/list/listinfo/curl-library
Etiquette: https://curl.se/mail/etiq
Henrik Holst via curl-library
2021-04-26 17:18:19 UTC
Permalink
HI,

libcurl handles the data you point to with CURLOPT_POSTFIELDS as raw byte
data, or rather "ibcurl will not convert or encode it for you in any way"
as the doc says. Could be that your webserver requires a different
Content-Type header to receive it correctly.

If you use http then I would fire up a network sniffer if I where you to
first make sure that what it sent on the wire is the hexbytes that I expect
them to be, if they are not then you should check your code for problems,
if they do match then it's time to check the server.

/HH

Den mån 26 apr. 2021 kl 18:52 skrev Milen Nikolov via curl-library <
Post by Milen Nikolov via curl-library
Hi,
thanks for the answer, I got a few more suggesting the same option. I
tried it today and it worked fine. I have a follow up question though. In
the string I'm sending there are extended ASCII characters. I'm currently
sending them to a test server and what I'm getting back is incorrect.
However since I haven't set up the server it's hard for me to know if the
problem is with curl or with the way the server is set up, so I wanted to
know if the extended ASCII chars in the POST field are getting sent
correctly. I tried searching but I only found suggestions to URL encode
them, which in my case is impossible, since the server is supposed to read
the HEX value of the symbol using the Latin 1 encoding.
Best Regards,
Milen
Post by Daniel Stenberg via curl-library
Post by Milen Nikolov via curl-library
I'm using libcurl in C++ and I have to make a post request to a
server,
Post by Daniel Stenberg via curl-library
Post by Milen Nikolov via curl-library
containing a string in the body section. Another -condition is that I
have
Post by Daniel Stenberg via curl-library
Post by Milen Nikolov via curl-library
to specify the content-type as octet-stream. I've set the header,
authentication and so on, but there is one problem: the curl function
(CURLOPT_POSTFIELDS) takes as argument a c-string. In my string there
are
Post by Daniel Stenberg via curl-library
Post by Milen Nikolov via curl-library
also NUL characters, which have to be sent. When I try sending the
request
Post by Daniel Stenberg via curl-library
Post by Milen Nikolov via curl-library
the body is truncated at the first NUL chararcter.
Set CURLOPT_POSTFIELDSIZE to the size of the data and it will not do
strlen()
Post by Daniel Stenberg via curl-library
to figure out the size.
I also just filed https://github.com/curl/curl/pull/6943 to clarify
this
Post by Daniel Stenberg via curl-library
better.
--
/ daniel.haxx.se
| Commercial curl support up to 24x7 is available!
| Private help, bug fixes, support, ports, new features
| https://www.wolfssl.com/contact/
-------------------------------------------------------------------
Unsubscribe: https://cool.haxx.se/list/listinfo/curl-library
Etiquette: https://curl.se/mail/etiquette.html
Patrick Monnerat via curl-library
2021-04-23 15:17:29 UTC
Permalink
Post by Milen Nikolov via curl-library
the curl function (CURLOPT_POSTFIELDS) takes as argument a c-string.
In my string there are also NUL characters, which have to be sent.
When I try sending the request the body is truncated at the first NUL
chararcter.
Did you try https://curl.se/libcurl/c/CURLOPT_POSTFIELDSIZE.html ?

-------------------------------------------------------------------
Unsubscribe: https://cool.haxx.se/list/listinfo/curl-library
Etiquette: https://curl.s
Christoph M. Becker via curl-library
2021-04-23 15:24:04 UTC
Permalink
Hi,
Post by Milen Nikolov via curl-library
I'm using libcurl in C++ and I have to make a post request to a server, containing a string in the body section. Another -condition is that I have to specify the content-type as octet-stream. I've set the header, authentication and so on, but there is one problem: the curl function (CURLOPT_POSTFIELDS) takes as argument a c-string. In my string there are also NUL characters, which have to be sent. When I try sending the request the body is truncated at the first NUL chararcter. I've tried saving my string in a char array and then providing it as an argument, but this didn't solve the issue, although if I try to print out chars after the first NUL in the console, they print out fine. So my question is if there's a workaround, which will allow me to solve the issue or is it impossible due to the fact that curl is a C library and the null-terminated strings are the C standard?
Did you try to set the desired length with CURLOPT_POSTFIELDSIZE?

--
Christoph M. Becker

-------------------------------------------------------------------
Unsubscribe: https://cool.haxx.se/list/listinfo/curl-library
Etiquette: https:
Loading...