curl / Mailing Lists / curl-users / Single Mail

curl-users

Re: Implicit message type with curl_easy_perform, and how to CURLOPT_PUT/CURLOPT_UPLOAD?

From: Ray Satiro <raysatiro_at_yahoo.com>
Date: Wed, 10 Apr 2019 19:28:33 -0400

On 4/10/2019 4:07 PM, Kent Williams wrote:
>
> 1. I make a CURL instance with curl_easy_init
> 2. If there's postdata to send, I use
> CURLOPT_POSTFIELDS/CURLOPT_POSTFIELDSIZE to set the post data.
>
> If I skip step 2, it looks like curl_easy_init uses CURLOPT_HTTPGET.
> If I do set CURLOPT_POSTFIELDS, curl_easy_init uses CURLOPT_HTTPPOST.
>
> Is it also the case that if I use CURLOPT_READDATA and
> CURLOPT_INFILESIZE to set put data, curl_easy_init will implicitly use
> CURLOPT_UPLOAD?  Or to I need to set CURLOPT_UPLOAD first?
>
> So is there any way to send a character string with CURLOPT_UPLOAD,
> besides writing the string to a temporary file and specifying
> CURLOPT_READDATA to be an open FILE * for the temp file, and
> CURLOPT_INFILESIZE to be the length of the string?

CURLOPT_READFUNCTION/CURLOPT_READDATA does not implicitly set
CURLOPT_UPLOAD, if it did the doc would say so.

There is no dedicated option to PUT a string in a resource. The easiest
way (but not the most correct) is piggyback off a POST option and
disable (or change) the content-type and method.

struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type:");
if(!headers)
  out of memory;
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "your string");
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
// Disable redirects, they're not handled as expected when using
customrequest.
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 0L);
curl_easy_setopt(curl, CURLOPT_URL, "http://httpbin.org/put");

However the most correct way (so that redirects and maybe some types of
auth would work properly) would be use CURLOPT_READFUNCTION and
CURLOPT_SEEKFUNCTION [1] together. The closest thing I think we have is
example ftpuploadfrommem.c [2]. Ideally you'd want to allow curl to seek
the position as well, which would involve keeping the starting point of
the string with the length and current position rather than the amount
left to upload (sizeleft). That's because if authentication or redirect
then libcurl may decide to rewind the data (ie you reset current
position) and try again.

[1]: https://curl.haxx.se/libcurl/c/CURLOPT_SEEKFUNCTION.html
[2]:
https://github.com/curl/curl/blob/curl-7_64_1/docs/examples/ftpuploadfrommem.c#L41-L65

-----------------------------------------------------------
Unsubscribe: https://cool.haxx.se/list/listinfo/curl-users
Etiquette: https://curl.haxx.se/mail/etiquette.html
Received on 2019-04-11