Server get body of request

101 views
Skip to first unread message

Ferdinand Thiessen

unread,
Apr 16, 2016, 12:42:30 PM4/16/16
to The C++ Network Library
Hello I have a very simple server, basically the example server.
I now want to get the body of the message (in my case json). The documentation says it should work with:
request.body or body(request).
But nothing of this works, the resulting string is always empty (I checked the client, but it is correctly sending the request).

My server handle looks like this:
class ServerHandle {
public:
ServerHandle(/* ... */){};
virtual ~ServerHandle(){};
void operator() (const server::request request, server::connection_ptr connection) {
std::string str = request.body;
std::cout << "'" << str << "'" << std::endl;
};
private:
//...
}

Here the complete file:
http://pastie.org/10799656

Do you know what I am doing wrong?
Regards,
Ferdinand

Dean Michael Berris

unread,
Apr 18, 2016, 6:35:34 AM4/18/16
to cpp-n...@googlegroups.com
On 16 Apr 2016, at 20:42, Ferdinand Thiessen <f.thi...@gmx.de> wrote:

Hello I have a very simple server, basically the example server.
I now want to get the body of the message (in my case json). The documentation says it should work with:
request.body or body(request).

Huh, that part is true but really misleading. It works for the synchronous client implementation, because that's what used to be the default. For the asynchronous implementation we have now (the only one) in 0.12.0, this will always return an empty string.

But nothing of this works, the resulting string is always empty (I checked the client, but it is correctly sending the request).

My server handle looks like this:
class ServerHandle {
public:
   ServerHandle(/* ... */){};
   virtual ~ServerHandle(){};
   void operator() (const server::request request, server::connection_ptr connection) {
   std::string str = request.body;
   std::cout << "'" << str << "'" << std::endl;
   };
private:
//...
}

Here the complete file:
http://pastie.org/10799656

Do you know what I am doing wrong?

So in the asynchronous client implementation, you're meant to interact with the connection. The latest documentation shows how to use the read function on the connection object:


Does this help?

Ferdinand Thiessen

unread,
Apr 19, 2016, 2:54:10 PM4/19/16
to The C++ Network Library


Am Montag, 18. April 2016 06:35:34 UTC+2 schrieb Dean Michael Berris:

....

Huh, that part is true but really misleading. It works for the synchronous client implementation, because that's what used to be the default. For the asynchronous implementation we have now (the only one) in 0.12.0, this will always return an empty string.

 
Got it from here: pod-server-request-concept

 
So in the asynchronous client implementation, you're meant to interact with the connection. The latest documentation shows how to use the read function on the connection object:


Does this help?

A bit, but at the moment it I stuck compiling, with the error:
async_connection.hpp|395|error: no match for call to ‘(const read_callback_function {aka const std::function<void(boost::iterator_range<const char*>, std::error_code, long unsigned int, std::shared_ptr<boost::network::http::async_connection<boost::network::http::tags::http_server, {anonymous}::serverhandle> >)>}) (std::error_code&, size_t&)’|

Having a look at this line: async_connection.hpp#L395
I looks there is a issue, it should be called with 4 parameters?

Dean Michael Berris

unread,
Apr 19, 2016, 3:00:22 PM4/19/16
to cpp-n...@googlegroups.com
Oh, that's bad. Looks like you're right -- it needs to be called with more parameters. :/

This is sufficiently bad that I'd need to fix it in the 0.12-release branch and the master branch. :(

Can you file an issue so that I can track and fix it ASAP?

Cheers

Ferdinand Thiessen

unread,
Apr 19, 2016, 5:41:50 PM4/19/16
to The C++ Network Library
Hm it looks like my fix works, but either I am using read wrong or there is a issue in the if() ... above that line.

Using read like this:

[&body](server::connection::input_range ir, std::error_code ec, std::size_t s, server::connection_ptr con) {
       body
.append(ir.begin(), ir.end());
       std
::cout << " FO: " << std::distance(ir.begin(), ir.end()) << std::endl;
       std
::cout << "Size: " << s << " cont: ";
       
for (auto s : ir)
            std
::cout << s;
       std
::cout << " END!!!" << std::endl;
});

does not work, sending "{}" as body it gives me: s = 2 but distance = 683.

The printed body begins correctly (starts with "{}"), but then it is followed with some crap like "<charset>.....".
Looks like something with the "new_start". Of cause using not a range-based for-loop and using for (size_t i = 0; i < s; i++) works.
So I am not sure, is this my fault or a real issue?

Dean Michael Berris

unread,
Apr 19, 2016, 7:12:09 PM4/19/16
to cpp-n...@googlegroups.com
Can you try patching in the pull request I just sent?


That should do the trick based on local testing with the example fileserver.

Cheers

--
You received this message because you are subscribed to the Google Groups "The C++ Network Library" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cpp-netlib+...@googlegroups.com.
To post to this group, send email to cpp-n...@googlegroups.com.
Visit this group at https://groups.google.com/group/cpp-netlib.
For more options, visit https://groups.google.com/d/optout.

Ferdinand Thiessen

unread,
Apr 19, 2016, 7:45:50 PM4/19/16
to The C++ Network Library
Thank you, that works!
I do not have tested the file-server, but my own testing code now works :-)
I have left a small inline comment in your diff. You do not need the "self" variable anymore there.

Dean Michael Berris

unread,
Apr 20, 2016, 5:59:39 AM4/20/16
to cpp-n...@googlegroups.com
On 20 Apr 2016, at 03:45, Ferdinand Thiessen <f.thi...@gmx.de> wrote:
>
> Thank you, that works!

You're welcome! Always happy to help!

> I do not have tested the file-server, but my own testing code now works :-)

That's better than testing the fileserver. :)

> I have left a small inline comment in your diff. You do not need the "self" variable anymore there.

Appreciate it. We can do the cleanup later for these implementations. In the meantime, I'm going to work on an 0.12.1-rc0 with updates to the documentation.

I'll announce something here when that happens.

Cheers
Reply all
Reply to author
Forward
0 new messages