Search code examples
curllibcurlmime

mime api alternative for libcurl's CURLFORM_BUFFER field


Recently I switched to curl 7.59 and am in the process of replacing form data upload with mime upload as suggested by curl docs. I am not able to find an equivalent mime api to set the CURLFORM_BUFFER field, which is currently used to set the file name field in my application. Anyone know which mime api to use (anything similar to curl_mime_data(CURLFORM_BUFFERPTR, CURLFORM_BUFFERLENGTH) or curl_mime_name(CURLFORM_COPYNAME)) ?

This is the mime example I am referring to: https://curl.haxx.se/libcurl/c/smtp-mime.html

As per curl docs: CURLFORM_BUFFER

is used for custom file upload parts without use of CURLFORM_FILE. It tells libcurl that the file contents are already present in a buffer. The parameter is a string which provides the filename field in the content header.


Solution

  • I think you want curl_mime_filename(), which you can use to set a file name on a regular "part" to make it look and act like a file upload to the receiver.

    Example

    Upload an image from memory and make it appear as a file upload:

     curl_mime *mime;
     curl_mimepart *part;
    
     /* create a mime handle */
     mime = curl_mime_init(easy);
    
     /* add a part */
     part = curl_mime_addpart(mime);
    
     /* point out the image data buffer */
     curl_mime_data(part, imagebuf, imagebuf_len);
    
     /* set a file name to make it look like a file upload */
     curl_mime_filename(part, "image.png");
    
     /* set a name on the part */
     curl_mime_name(part, "my-avatar");