beast: How to receive data while http post without "bad chunk"?

While porting a client from curl to beast (v57 and v58) i am not able to receive data in rpc (http post).

class client_impl : public virtual client_base
{
public:
    client_impl(const std::string& host, const std::string& port)
    :
        _host(host),
        _service(),
        _socket(_service)
    {
        std::clog << BEAST_VERSION_STRING << std::endl;
        boost::asio::ip::tcp::resolver resolver{_service};
        boost::asio::ip::tcp::resolver::query query{host, port};
        boost::asio::connect(_socket, resolver.resolve(query));
    }

    virtual value_t call(const string_t& service, const string_t& method, const list_t& arguments) override
    {
        // Send HTTP request using beast
        beast::http::request<beast::http::string_body> request;
        request.method(beast::http::verb::post);
        request.target(service);
        request.insert(beast::http::field::host, _host);
        request.insert(beast::http::field::user_agent, "libhessian/1.0");
        request.insert(beast::http::field::accept, "*/*");
        request.insert(beast::http::field::content_type, "x-application/hessian");
        request.body = generate(method, arguments);
        request.prepare();
        std::clog << request << std::endl;
        beast::http::write(_socket, request);

        // Receive and print HTTP response using beast
        beast::flat_buffer buffer;
        beast::http::response<beast::http::string_body> response;
        beast::http::read(_socket, buffer, response); // ERROR HERE
        std::clog << response << std::endl;

        const content_t content = parse(response.body);
        return boost::apply_visitor(content_visitor(), content);
    }

    virtual bool negotiate(const string_t& resource) override
    {
        throw std::runtime_error("beast http does not support spnego.");
    }

private:
    std::string _host;
    boost::asio::io_service _service;
    boost::asio::ip::tcp::socket _socket;
};

The request seems to be ok (same as in curl):

POST /test/test2 HTTP/1.1
Host: hessian.caucho.com
User-Agent: libhessian/1.0
Accept: */*
Content-Type: x-application/hessian
Content-Length: 15

HC	replyTrue�

But while reading the response i always get “bad chunk” error.

Verbose info from same call using curl:

*   Trying 54.235.109.78...
* TCP_NODELAY set
* Connected to hessian.caucho.com (54.235.109.78) port 80 (#0)
> POST /test/test2 HTTP/1.1
Host: hessian.caucho.com
User-Agent: libhessian/1.0
Accept: */*
Content-Type: x-application/hessian
Content-Length: 15

* upload completely sent off: 15 out of 15 bytes
< HTTP/1.1 200 OK
< Server: Resin/4.0.s150303
< Content-Type: x-application/hessian
< Transfer-Encoding: chunked
< Date: Thu, 15 Jun 2017 14:59:50 GMT
< 
* Curl_http_done: called premature == 0
* Connection #0 to host hessian.caucho.com left intact

So i missed something in the impl?!

Best regard, Mike…

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 26 (26 by maintainers)

Commits related to this issue

Most upvoted comments

Execution times of example

  1. curl http
real	0m9,201s
user	0m0,024s
sys	0m0,008s
  1. beast http
real	0m4,570s
user	0m0,004s
sys	0m0,008s

Great job! Works like a charm 😃 Thank you!!