gpt4 book ai didi

c++ - Boost::ASIO HTTP POST 中的空主体

转载 作者:行者123 更新时间:2023-11-28 05:05:46 24 4
gpt4 key购买 nike

我尝试将 Post 方法与以下代码结合使用。在服务器端,正文总是空的。

tcp::endpoint ip_port(address::from_string(host), port);
socket.connect(ip_port);

boost::asio::streambuf request;
std::ostream request_stream(&request);

request_stream << "POST /myservice HTTP/1.1\n\n";
request_stream << "Host:" << "host:port" << "\r\n";
request_stream << "User-Agent: C/1.0" << "\r\n";
request_stream << "Content-Type: application/json; charset=utf-8\r\n";
request_stream << "Accept: */*\r\n";
request_stream << "Content-Length: ";
request_stream << json.length() + "\r\n";
request_stream << "Connection: close\r\n\r\n";
request_stream << json;

boost::asio::write(socket, request);

我通过从其他客户端发送请求来检查服务器端,它工作正常。请让我知道我做错了什么。

最佳答案

终于找到了。第一行\n\n 的问题。如果有两个\n,请求到此结束。我尝试了下面的代码,我能够在服务器端获取 json 正文。

requestStream << "POST " << "/myservice" << " HTTP/1.1\r\n";
requestStream << "Host: " << "myhost" << "\r\n";
requestStream << "Accept: application/json\r\n";
requestStream << "Content-Type: application/json; charset=UTF-8\r\n";
requestStream << "Content-Length: " << json.length() << "\r\n";
requestStream << "\r\n";
requestStream << json << "\n\n";

关于c++ - Boost::ASIO HTTP POST 中的空主体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44817842/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com