Add support for sending files in HTTP forms.

This commit is contained in:
tastytea 2020-01-11 14:27:10 +01:00
parent 18de12c762
commit 8d04f7e9ea
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
3 changed files with 16 additions and 5 deletions

View File

@ -297,9 +297,9 @@ private:
/*!
* @brief Add `*curl_mimepart` to `*curl_mime`.
*
* @param mime Initialized `*curl_mime`.
* @param name Name of the field.
* @param data Data of the field, or \@filename.
* @param mime Initialized `*curl_mime`. @param name Name of the field.
* @param data Data of the field. If it begins with <tt>`\@file:<tt>, the
* rest of the ergument is treated as a filename.
*
* @since 0.1.1
*/

View File

@ -69,7 +69,10 @@
*
* @subsection input Input
*
* All text input is expected to be UTF-8.
* * All text input is expected to be UTF-8.
*
* * To send a file, use <tt>\@file:</tt> followed by the file name as value
* in the @link mastodonpp::parametermap parametermap@endlink.
*
* @section exceptions Exceptions
*

View File

@ -354,7 +354,15 @@ void CURLWrapper::add_mime_part(curl_mime *mime,
throw CURLException{code, "Could not build HTTP form."};
}
code = curl_mime_data(part, data.data(), CURL_ZERO_TERMINATED);
if (data.substr(0, 6) == "@file:")
{
const string_view filename{data.substr(6)};
code = curl_mime_filedata(part, filename.data());
}
else
{
code = curl_mime_data(part, data.data(), CURL_ZERO_TERMINATED);
}
if (code != CURLE_OK)
{
throw CURLException{code, "Could not build HTTP form."};