简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM