【问题标题】:Unable to send post request via cpp-netlib无法通过 cpp-netlib 发送发布请求
【发布时间】:2012-10-20 11:31:58
【问题描述】:

我在 Visual Studio 2010 中使用 cpp-netlib-0.9.4。我有一个函数 make_header,如下所示:

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;
}

get 请求可以正常工作,类似于:

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

但是 POST 请求会引发异常/核心转储。否则我已经检查了多次,它似乎每次都在 chrome dev http 客户端扩展上工作。我用于发布请求的 URL 是:

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

在上面的例子中,我尝试

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.

有什么想法吗?

【问题讨论】:

    标签: c++ cpp-netlib


    【解决方案1】:

    查询参数不能包含空格,您必须对其进行 URL 编码。

    空格是%20,您的查询应该是这样的

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-11
      • 2014-10-09
      • 1970-01-01
      • 2019-10-08
      相关资源
      最近更新 更多