【问题标题】:Empty body in Boost::ASIO HTTP POSTBoost::ASIO HTTP POST 中的空正文
【发布时间】:2017-06-29 06:29:51
【问题描述】:

我尝试在下面的代码中使用 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);

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

【问题讨论】:

  • 也许在boost::asio::write之前添加request_stream &lt;&lt; std::flush;
  • @Mankarse 试过了,遇到同样的问题。

标签: c++ http boost


【解决方案1】:

终于找到了。第一行 \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";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-26
    • 2013-09-01
    • 2011-08-06
    • 2019-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-09
    相关资源
    最近更新 更多