我想將 json 資料傳輸到 cpp 中的 json boost 請求中。
如果我在 boost 中使用 json
int outer=2;
value data = {
{"dia",outer},
{"sleep_time_in_s",0.1}
};
request.body()=data;
像上面一樣,我想將資料從 boost 客戶端發送到服務器,但它是通過錯誤是任何人理解下面的錯誤建議我。
error C2679: binary '=': no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
C:\Boost\boost/beast/core/multi_buffer.hpp(360,25): message : could be 'boost::beast::basic_multi_buffer<std::allocator<char>> &boost::beast::basic_multi_buffer<std::allocator<char>>::operator =(const boost::beast::basic_multi_buffer<std::allocator<char>> &)'
2>C:\Boost\boost/beast/core/multi_buffer.hpp(345,5): message : or 'boost::beast::basic_multi_buffer<std::allocator<char>> &boost::beast::basic_multi_buffer<std::allocator<char>>::operator =(boost::beast::basic_multi_buffer<std::allocator<char>> &&)'
2>C:\Development\qa-cal\sysqa_cpp\src\guiClient\boostHttpClient.cpp(90,31): message : while trying to match the argument list '(boost::beast::basic_multi_buffer<std::allocator<char>>, std::string)'
uj5u.com熱心網友回復:
C 是強型別的。您不能將 a 分配json::value給其他東西。在這種情況下,您body()的可能類似于std::string.
假設您應該撰寫如下value內容:boost::json::value
request.body() = serialize(data);
將值boost::json::serialize序列data化為字串表示形式。
現場演示
#include <boost/beast.hpp>
#include <boost/json.hpp>
#include <boost/json/src.hpp>
#include <iostream>
namespace http = boost::beast::http;
int main() {
using boost::json::value;
http::request<http::string_body> request(
http::verb::post, "/some/api", 11);
{
int outer = 2;
value data = {
{"dia", outer},
{"sleep_time_in_s", 0.1},
};
request.body() = serialize(data);
}
request.prepare_payload();
std::cout << request;
}
印刷
POST /some/api HTTP/1.1
Content-Length: 32
{"dia":2,"sleep_time_in_s":1E-1}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/479441.html
