Search code examples
c++cpp-netlib

Unable to send post request via cpp-netlib


I am using cpp-netlib-0.9.4 with Visual Studio 2010. I have a function make_header which is like this:

http::client::request* Interface::make_request_header(const string& uri) {
    string url = host_ + uri;
    string json_type = "application/json";
    http::client::request* req = new http::client::request(url);
    req->add_header(make_pair("X-AUTH-TOKEN", token_));
    req->add_header(make_pair("Content-Type", json_type));
    req->add_header(make_pair("Accepts", json_type));
    return req;
}

A get request works perfectly fine, which is something like:

http::client client;
http::client::request* req = make_request_header(my_uri);
http::client::response res = client.get(*req);

But a POST request throws an exception/core-dump. I have checked it multiple times otherwise and it seems to work every time on chrome dev http client extension. The URL I use for post request is:

http://myhost.com/commands?query=my query

In the above example, I try

http::client client;
http::client::request* req = make_request_header("http://myhost.com/commands?query=my query");
http::client::response res = client.post(*req);   // FAILS AT THIS STEP.

Any ideas why?


Solution

  • A query parameter can not contain spaces, you have to URL-encode it.

    Space is %20 to your query should look like

    http://myhost.com/commands?query=my%20query