Sending a file from memory (not disk) via HTTP using libcurl

I would like to send photos through a program written in C ++. - OK It works, but I would like to send the images with the preloaded carrier to the char variable (do you know what I mean? First I load the pictures into the variable, and then send the variable), because now I have to specify the path images on disk.

I wanted to write this program in C ++ using the curl library, and not through exe. expansion. I also found such a program (which was modified by me a little)

+4
source share
2 answers

CURLFORM_PTRCONTENTS is not correct use here, it will not create part of the file upload.

Instead, use CURLFORM_BUFFER to send an image from an existing buffer to memory.

curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "send", CURLFORM_BUFFER, "nowy.jpg", CURLFORM_BUFFERPTR, data, CURLFORM_BUFFERLENGTH, size, CURLFORM_END); 
+9
source

Read the documentation for curl_formadd : http://curl.haxx.se/libcurl/c/curl_formadd.html

In particular, in the "Parameters" section:

CURLFORM_PTRCONTENTS

followed by a pointer to the contents of this part, the actual data to send is far away. libcurl will use a pointer and access the data in your application, so you need to make sure that it remains until the curl is no longer needed. If the data does not end with NUL or if you want it should contain zero bytes, you must set its length to CURLFORM_CONTENTSLENGTH.

CURLFORM_CONTENTSLENGTH

followed by a long job of content length. Note that for CURLFORM_STREAM, this parameter is required.

So instead

  curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "send", CURLFORM_FILE, "nowy.jpg", CURLFORM_END); 

You need something like

  curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "send", CURLFORM_PTRCONTENTS, p_jpg_data, CURLFORM_CONTENTSLENGTH, jpg_data_len, CURLFORM_END); 

I assume that you know how to create p_jpg_data and read the data in it, or do you need to explain this?

+1
source

Source: https://habr.com/ru/post/1305301/


All Articles