Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I send long PUT data in libcurl without using file pointers?

I'm trying to interface with the Google Contact API, and in order to update a contact on the remote side, I need to use a PUT request instead of POST.

Since the data I want to PUT is already in memory, I really don't want to deal with file pointers, which seems to be the default behavior of CURLOPT_READDATA. Thus, I understand I need to use CURLOPT_READFUNCTION in addition to that but I'm failing to understand the logic.

Here is the relevant doc: http://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTREADFUNCTION

Printing the variables passed in, I see nmemb=16372 and size=1. If my data is going to be of size 100K, for example, what is the logic that I need to implement?

Thank you.

like image 221
Artem Russakovskii Avatar asked Aug 25 '10 03:08

Artem Russakovskii


People also ask

What does CURL_ easy_ setopt do?

curl_easy_setopt is used to tell libcurl how to behave. By setting the appropriate options, the application can change libcurl's behavior. All options are set with an option followed by a parameter.

Does libcurl work with C ++?

Client URL, or just curl, is a command-line tool for transferring data using various network protocols. It is commonly used by developers to test various applications build on top of HTTP. That said, curl itself is just a wrapper around libcurl.

How does libcurl work?

libcurl tries to keep a protocol independent approach to most transfers, thus uploading to a remote FTP site is similar to uploading data to an HTTP server with a PUT request. Of course, first you either create an easy handle or you re-use one existing one. Then you set the URL to operate on just like before.

What is curl library in C?

cURL (pronounced like "curl", UK: /kəːl/, US: /kɝl/) is a computer software project providing a library (libcurl) and command-line tool (curl) for transferring data using various network protocols. The name stands for "Client URL".


2 Answers

You will still want to use CURLOPT_READDATA, however if you use CURLOPT_READFUNCTION, it can be any user-specified pointer. You can create a simple structure like:

struct put_data
{
  char *data;
  size_t len;
};

where data is the PUT data and len is the length (remaining).

Then, set CURLOPT_READDATA to a pointer to an initialized instance of that structure. You will be passed it in CURLOPT_READFUNCTION as userdata. In that function, do something like:

size_t curl_size = nmemb * size;
size_t to_copy = (userdata->len < curl_size) ? userdata->len : curl_size;
memcpy(ptr, userdata->data, to_copy);
userdata->len -= to_copy;
userdata->data += to_copy;
return to_copy;

This basically figures out the amount to copy, copies it, then updates the length and pointer. On the to_copy line, we calculate the minimum because were are bounded by both the amount remaining and the size of curl's buffer. Finally, we return the number of bytes copied, as required by curl. When you're at the end of content user_data->len (and thus to_copy) will be 0. Nothing will be copied and returning 0 ends the transfer.

like image 98
Matthew Flaschen Avatar answered Oct 13 '22 12:10

Matthew Flaschen


THIS is the right way to do it.

One more thing to keep in mind is that you MUST set the FILE SIZE:

curl_easy_setopt(curlHandle, CURLOPT_INFILESIZE,(curl_off_t)putData.size);

Otherwise your server might throw an error stating that the request length wasn't specified.

putData is an instance of the put_data structure.

like image 37
eco Avatar answered Oct 13 '22 14:10

eco