Note that there are some explanatory texts on larger screens.

plurals
  1. POsend http request from tornado client to asynchronous cpp-netlib server
    primarykey
    data
    text
    <p>I have a http client that I wrote in python in tornado framework:</p> <pre class="lang-py prettyprint-override"><code>http_client = httpclient.HTTPClient() request = httpclient.HTTPRequest("http://127.0.0.1:8000", method='PUT', body=str("data")) response = http_client.fetch(request) </code></pre> <p>on the other side I have an asynchronous server that I wrote in c++ using cpp-netlib. It basically has to read the request and print its body</p> <pre class="lang-cpp prettyprint-override"><code>class Server; typedef http::async_server&lt;Server&gt; server; class Server { public: void operator()(server::request const &amp; request, server::connection_ptr connection) { boost::shared_ptr&lt;connection_handler&gt; h(new connection_handler()); (*h)(request, connection); server::response_header headers[] = { {"Connection","close"} ,{"Content-Type", "text/plain"} }; connection-&gt;set_headers(boost::make_iterator_range(headers, headers+2)); connection-&gt;set_status(server::connection::accepted); connection-&gt;write("helloworld"); } int main() { Server handler; server::options options(handler); server instance( options.thread_pool(boost::make_shared&lt;utils::thread_pool&gt;(5)) .address("0.0.0.0") .port("8000")); instance.run(); return 0; } </code></pre> <p>and the connection handler looks like this:</p> <pre class="lang-cpp prettyprint-override"><code>struct connection_handler : boost::enable_shared_from_this&lt;connection_handler&gt; { struct read_post_callback { typedef boost::shared_ptr&lt;connection_handler&gt; handler_ptr_t; read_post_callback(handler_ptr_t handler_ptr) : handler(handler_ptr) {} void operator()(server::connection::input_range range, boost::system::error_code error, size_t size, server::connection_ptr conn) { handler-&gt;read_sofar += size; handler-&gt;cond.notify_one(); } handler_ptr_t handler; }; void operator()(server::request const &amp;req, server::connection_ptr conn) { int cl; server::request::headers_container_type const &amp;hs = req.headers; for(server::request::headers_container_type::const_iterator it = hs.begin(); it!=hs.end(); ++it) { if(boost::to_lower_copy(it-&gt;name)=="content-length") { cl = boost::lexical_cast&lt;int&gt;(it-&gt;value); break; } } cout&lt;&lt; req.body &lt;&lt; endl; read_sofar = 0; while(read_sofar&lt;cl) { boost::unique_lock&lt;boost::mutex&gt; lock(mtx); server::connection::read_callback_function cc = read_post_callback(shared_from_this()); conn-&gt;read(cc); cond.wait(lock); } } int read_sofar; boost::condition_variable cond; boost::mutex mtx; }; </code></pre> <p>But request body is always empty and instead of the response I send back ("hello world") most of the time I get something like this (the number could be different)</p> <blockquote> <p>Error: HTTP 0: Unknown</p> </blockquote> <p>Can you tell me why the body is empty or why the response doesn't reach?</p> <p><strong>EDIT:</strong></p> <p>Why the body was empty is in the answer. What I was doing wrong that I wouldn't receive the response was this: I was setting the connection status after setting connection headers in my server. Just swapped their order and it worked like a charm.</p> <pre><code>server::response_header headers[] = { {"Connection","close"} ,{"Content-Type", "text/plain"} }; connection-&gt;set_status(server::connection::accepted); connection-&gt;set_headers(boost::make_iterator_range(headers, headers+2)); connection-&gt;write("helloworld"); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload