CppRestSDK 如何发布多部分数据
CppRestSDK How to POST multipart data
我正在尝试将多部分数据发布到服务器,我正在从CPR切换到CPPRestSDK,但我似乎找不到任何文档
来自心肺复苏术,https://github.com/whoshuu/cpr
这意味着我已经尝试了该代码,但我似乎找不到有关 cpprestsdk 的任何多部分数据文档。
cpr::Multipart multipart_data{};
for (size_t i = 0; i < files.size(); i++) {
if (!is_image_or_gif(files[i].filepath)) {
std::string entire_file = read_entire_file(files[i].filepath);
std::string custom_filename{ files[i].spoiler ? "SPOILER_" : "" };
multipart_data.parts.emplace_back(
"file" + std::to_string(i),
cpr::Buffer{ entire_file.begin(),
entire_file.end(),
custom_filename + files[i].filename },
"application/octet-stream");
} else {
multipart_data.parts.emplace_back("file" + std::to_string(i),
cpr::File(files[i].filepath),
"application/octet-stream");
}
}
auto payload_json = nlohmann::json{
{ "content", content },
{ "tts", tts }
}.dump();
multipart_data.parts.emplace_back("payload_json", payload_json);
auto response = cpr::Post(
cpr::Url{ endpoint("/channels/%/messages", id) },
cpr::Header{ { "Authorization", format("Bot %", discord::detail::bot_instance->token) },
{ "Content-Type", "multipart/form-data" },
{ "User-Agent", "DiscordBot (http://www.github.com/yuhanun/dpp, 0.0.0)" },
{ "Connection", "keep-alive" } },
multipart_data);
file
结构非常明显的地方。
标题,很好,我想通了,我只需要一些帮助发送多部分数据基本上:)
我的预期结果是让服务器使用"成功"json(在本例中为已发送消息的消息对象(进行响应,但是,现在,我什至不知道从哪里开始。
由于出于某种原因,这得到了一些赞成票,我想回答这个问题。
我很久以前就解决了这个问题,您可以查看我的存储库以了解确切的方法。
https://github.com/Yuhanun/DPP/blob/master/src/channel.cpp#L108
https://github.com/Yuhanun/DPP/blob/master/src/utils.cpp#L180
享受解决方案。
这是
可以做到的:
// Form a multipart/form-data scheme
// 1.Create a boundary ( a random value that you know is unique)
// KabezOf... is something goofie I just wrote, make sure you use
// something that you know does not exist in the actual messages.
std::string boundary = "----KabezOfFireDegelDegelGoGoniDaijobo";
// 2.Embed different fields
std::stringstream bodyContent;
std::map<std::string, std::string> keyValues{ {"key1", "value1"},
{"key2", "value2"},
{"key3", "value3" } };
for (auto& [key, value] : keyValues)
{
bodyContent << "--" << boundary << "rn"
<< "Content-Disposition: form-data; name="" << key << ""rnrn"
<< value << "rn";
}
// 3. if you have any files to send as well, Now is the time
// here we are sending an image file, as you can see, like
// previous block, you can put this into a loop and instead
// of 1 file, have several files.
// reading the image
std::ifstream inputFile;
inputFile.open(imagePath, std::ios::binary | std::ios::in);
std::ostringstream imgContent;
std::copy(std::istreambuf_iterator<char>(inputFile),
std::istreambuf_iterator<char>(),
std::ostreambuf_iterator<char>(imgContent));
auto imageFilename = std::filesystem::path(imagePath).filename().string();
bodyContent << "--" << boundary<<"rn"
<< "Content-Disposition: form-data; name="image"; filename="" << imageFilename << ""rn"
<< "Content-Type: " << contentType << "rnrn"
<< imgContent.str() << "rn"
<< "--" << boundary << "--";
web::http::client::http_client client(your_uri);
web::http::http_request requestMessage{ methods::POST };
requestMessage.set_body(bodyContent.str(), "multipart/form-data; boundary=" + boundary);
auto result = client.request(requestMessage);
相关文章:
- 通过套接字[TCP]传输数据 如何在C / C ++中打包多个整数并使用send() recv()传输数据
- 具有多个模板的模板函数,用于特定数据类型(如字符串)?
- 如何实现高效的算法来计算大型数据集的多个不同值?
- 与多个 for 循环与单个 for 循环 wrt 相关的性能从多映射获取数据
- 多维数组存储三种不同的数据类型?
- C++:在多个线程中访问同一数组/向量的不同单元格是否会产生数据竞赛?
- 在PROGMEM中添加更多数据会中断Arduino Mega 2560上的SPI传输
- C++中的多维数据集:从 std::vector 的 2D 数据到 std::vector 的 2D 网格的最干净方法?
- 为什么添加延迟会提高此多线程环境中的数据吞吐量?
- 是否可以跨多个源文件构建 constexpr 数据结构?
- 将数据发送到多个客户端 UDP 时不支持地址族
- C++多态数据结构
- C++,在对象内分配多个数据时,堆栈分配是否更有效? 在下面的程序中,类A_Heap的效率会更低吗?
- 将多个效果与 libSox 链接并读取输出数据的正确方法
- 用于循环访问多个集合的数据结构
- 为什么 std::set 容器使用的内存比其数据大小多得多?
- 如何有效地实现将向量的数据分配给多个变量?
- 如何从多维向量中提取数据
- 多维数据集ai中的ai_float是什么
- 无法构造具有给定数据类型的多映射