|
1 | 1 | /* |
2 | 2 |
|
3 | 3 | Modern, non-blocking and exception free HTTP Client library for C++ (17+) |
4 | | -version 1.6.0 |
| 4 | +version 1.7.0 |
5 | 5 | https://github.com/leventkaragol/libcpp-http-client |
6 | 6 |
|
7 | 7 | If you encounter any issues, please submit a ticket at https://github.com/leventkaragol/libcpp-http-client/issues |
@@ -38,6 +38,7 @@ SOFTWARE. |
38 | 38 | #include <map> |
39 | 39 | #include <memory> |
40 | 40 | #include <mutex> |
| 41 | +#include <optional> |
41 | 42 | #include <sstream> |
42 | 43 | #include <curl/curl.h> |
43 | 44 |
|
@@ -234,6 +235,20 @@ namespace lklibs |
234 | 235 | return *this; |
235 | 236 | } |
236 | 237 |
|
| 238 | + /** |
| 239 | + * @brief Set the file data to be sent with the request as multipart/form-data |
| 240 | + * |
| 241 | + * @param name: Name of the form field |
| 242 | + * @param data: Path to the file to be sent |
| 243 | + * @param mimeType: Optional MIME type of the file |
| 244 | + */ |
| 245 | + HttpRequest& addMultipartFormData(const std::string& name, const std::string& data, const std::optional<std::string>& mimeType = std::nullopt) noexcept |
| 246 | + { |
| 247 | + this->formData[name] = FormData{data, mimeType}; |
| 248 | + |
| 249 | + return *this; |
| 250 | + } |
| 251 | + |
237 | 252 | /** |
238 | 253 | * @brief Set the timeout for the request |
239 | 254 | * |
@@ -393,9 +408,16 @@ namespace lklibs |
393 | 408 | "PATCH" |
394 | 409 | }; |
395 | 410 |
|
| 411 | + struct FormData |
| 412 | + { |
| 413 | + std::string data; |
| 414 | + std::optional<std::string> mimeType; |
| 415 | + }; |
| 416 | + |
396 | 417 | std::string url; |
397 | 418 | std::string method = "GET"; |
398 | 419 | std::string payload; |
| 420 | + std::map<std::string, FormData> formData; |
399 | 421 | std::string userAgent; |
400 | 422 | bool sslErrorsWillBeIgnored = false; |
401 | 423 | ReturnFormat returnFormat = ReturnFormat::TEXT; |
@@ -479,6 +501,31 @@ namespace lklibs |
479 | 501 | curl_easy_setopt(curl.get(), CURLOPT_POSTFIELDS, this->payload.c_str()); |
480 | 502 | } |
481 | 503 |
|
| 504 | + const std::unique_ptr<curl_mime, decltype(&curl_mime_free)> mime(curl_mime_init(curl.get()), &curl_mime_free); |
| 505 | + |
| 506 | + if (!this->formData.empty()) |
| 507 | + { |
| 508 | + for (const auto& [field, content] : this->formData) |
| 509 | + { |
| 510 | + std::unique_ptr<curl_mimepart, std::function<void(curl_mimepart*)>> part( |
| 511 | + curl_mime_addpart(mime.get()), |
| 512 | + [](curl_mimepart*) {}); |
| 513 | + |
| 514 | + curl_mime_name(part.get(), field.c_str()); |
| 515 | + if (content.mimeType.has_value()) |
| 516 | + { |
| 517 | + curl_mime_filedata(part.get(), content.data.c_str()); |
| 518 | + curl_mime_type(part.get(), content.mimeType->c_str()); |
| 519 | + } |
| 520 | + else |
| 521 | + { |
| 522 | + curl_mime_data(part.get(), content.data.c_str(), CURL_ZERO_TERMINATED); |
| 523 | + } |
| 524 | + } |
| 525 | + |
| 526 | + curl_easy_setopt(curl.get(), CURLOPT_MIMEPOST, mime.get()); |
| 527 | + } |
| 528 | + |
482 | 529 | if (dataCallback) |
483 | 530 | { |
484 | 531 | curl_easy_setopt(curl.get(), CURLOPT_WRITEFUNCTION, streamingWriteCallback); |
|
0 commit comments