Home > database >  How to pass wide characters to libcurl API which takes only const char*?
How to pass wide characters to libcurl API which takes only const char*?

Time:06-23

I'm using libcurl to upload some files to my server. I need to handle files whose names are in different languages e.g. Chinese, Hindi, etc. For that, I need to handle files using std::wstring instead of std::string.

libcurl has a function with a prototype like below:

CURLcode curl_mime_filename(curl_mimepart *part, const char *filename);

But I cannot pass std::wstring::c_str() because it will return const wchar_t* instead of const char*.

Edit: I still don't know what encoding scheme is used by the server as it is a third party app so I used std::wstring for handling filenames and the conversion from std::wstring to std::string is done by the below function I found on this forum.

std::string ws2s(const std::wstring& wstr)
{
    using convert_typeX = std::codecvt_utf8<wchar_t>;
    std::wstring_convert<convert_typeX, wchar_t> converterX;

    return converterX.to_bytes(wstr);
}

CodePudding user response:

According to the CURL wiki, curl_mime_filename expects filename to be in codepage determined by context on either the local or remote side. That means you have to ask server what encoding it uses and use that encoding too.

CodePudding user response:

curl_mime_filename is not suitable for

files whose names are in different languages e.g. Chinese, Hindi, etc.

It is suitable for ASCII and UTF-8 file name encodings only.

The job of curl_mime_filename is:

  1. Detect the data content type, for example file.jpgimage/jpeg.
  2. Add the multipart header Content-Disposition, for example Content-Disposition: attachment; name="data"; filename="file.jpg". The encoding to filename= is not set.

  • If you know the server encoding, then encode the chars 0x80 .. 0xff in filename to

  • Related