Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POST request body is empty,http sync server not async (0.12.0 final) #860

Open
choujayyl opened this issue Oct 17, 2018 · 1 comment
Open

Comments

@choujayyl
Copy link

choujayyl commented Oct 17, 2018

I test the http sync server example,but post request body is empty.
cpp-netlib version is cpp-netlib-0.12.0-final-104-gb284b14.

Here is my code:

#include <boost/network/protocol/http/server.hpp>
#include <iostream>

namespace http = boost::network::http;

/*<< Defines the server. >>*/
struct hello_world;
typedef http::server<hello_world> server;

/*<< Defines the request handler.  It's a class that defines two
     functions, `operator()` and `log()` >>*/
struct hello_world {
  /*<< This is the function that handles the incoming request. >>*/
  void operator()(server::request const &request, server::connection_ptr connection) {
    server::string_type ip = source(request);
    unsigned int port = request.source_port;
    //const auto  header = request.headers();

    for(const auto& header : request.headers)
    {
         std::cout<<"header.name:"<< header.name<<"headr.value:"<<header.value<< std::endl;
    }

     

> ////!!!! the body is empty!!!!

    std::cout<<"body:"<< body(request)<< std::endl;

    std::cout<<"request.uri():"<<std::string(destination(request))<< std::endl;

    std::ostringstream data;
    data << "Hello, " << ip << ':' << port << '!'<<" "<<bodystr;
    connection->write(data.str());
  }
};

int main(int argc, char *argv[]) {

  if (argc != 3) {
    std::cerr << "Usage: " << argv[0] << " address port" << std::endl;
    return 1;
  }

  try {
    /*<< Creates the request handler. >>*/
    hello_world handler;
    /*<< Creates the server. >>*/
    server::options options(handler);
    server server_(options.address(argv[1]).port(argv[2]));
    /*<< Runs the server. >>*/
    server_.run();
  }
  catch (std::exception &e) {
    std::cerr << e.what() << std::endl;
    return 1;
  }

  return 0;
}

So,why.

@choujayyl choujayyl changed the title POST request body is empty,http sync server not async POST request body is empty,http sync server not async (0.12.0 final) Oct 17, 2018
@deanberris
Copy link
Member

We no longer have a "sync" HTTP server in 0.12.0 -- you need to pull the data from the connection pointer. In this case the body is always empty, but the connection is still open, and you can use the read function to pull the data out:

https://cpp-netlib.org/0.12.0/reference/http_server.html#_CPPv2N5boost7network4http16async_connection4readE22read_callback_function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants