Answer the question
In order to leave comments, you need to log in
Why can't I get the whole post request body in c++ asio?
I'm trying to figure out the code of a small web server on asio link to the source . I had such a problem that I send a post request to this server with a small file in the body via postman, but only a small part of the request is accepted, I wanted to know why it is not possible to accept the entire request?
the part of the request i get
void HttpAcceptor::AcceptConnection()
{
std::shared_ptr<asio::ip::tcp::socket> sock(new asio::ip::tcp::socket(m_IOService));
m_Acceptor.async_accept(*sock.get(),
[this, sock](const boost::system::error_code& ec)
{
if(ec.value() == 0){
(new HttpService(sock))->HttpHandleRequest();
}else{
std::cout<<"Error occured, Error code = "<<ec.value()
<<" Message: "<<ec.message();
}
// accept next request if not stopped yet
if(!m_IsStopped.load()){
AcceptConnection();
}else{
//stop accepting incoming connection requests
m_Acceptor.close();
}
});
}
//код обработки запроса
void HttpService::HttpHandleRequest()
{
//read the request from client
asio::async_read_until(*m_Socket.get(),
m_Request, '\n',
[this](const boost::system::error_code& ec,
std::size_t bytes_transfered)
{
//get string from read stream
std::string request_line;
std::istream request_stream(&m_Request);
std::getline(request_stream, request_line, '\0');
//parse the string and get HTTP request
HttpRequestParser parser(request_line);
std::shared_ptr<HttpRequest> http_request = parser.GetHttpRequest();
std::cout<<"Request: {"<<std::endl;
std::cout<<http_request->request;
std::cout<<"}"<<std::endl;
SendResponse();
});
Answer the question
In order to leave comments, you need to log in
We look at the PNG format , the file header.
137 80 78 71 13 10 26 10
13 10 is nothing but \r\n.
And your code reads the request just before \n.
According to your mind, you need to read the headers until an empty string (\r\n\r\n) is encountered, then look for the Content-Length header and read the number of bytes specified in it.
Then, taking into account the Content-Type, restore the request from the read data.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question