如何通过卡萨布兰卡检索响应头

How to retrieve response header via casablanca?

本文关键字:响应 检索 卡萨布兰卡 何通过      更新时间:2023-10-16
wchar_t token[50];  
http_client client(L"https://example.com/dir/");
    http_request request;
    std::stringstream ReqBody;
    std::string ReqBodyS;
    ReqBody << "login=" << TB1T << "&pass=" << TB2T;
    ReqBodyS = ReqBody.str();
    request.set_body(ReqBodyS);
    request.set_method(methods::POST);
    request.headers().set_content_type(U("application/x-www-form-urlencoded"));
    client.request(request).then([](http_response response) {       
        if (response.status_code() == status_codes::OK)
        {
            //
        }
    });

反应像

Connection: keep-alive
Content-type: text/html
SomeHeader: something here

我如何添加文本从标题与名称SomeHeader令牌?我想从someheader

获取token文本

这是一个旧的帖子,但我会给你一些帮助,我假设你已经解决了你的问题很久以前,但如果没有,或者如果有人得到同样的问题,这里一些解释。

如果你想用c++ Rest SDK aka Casablanca获取请求头:

你有你的令牌头的属性,按照惯例它是"授权"。要获取令牌,只是一个基本的例子,您只需要这样做:

auto authorization = utility::conversions::to_utf8string(request.headers()[header_names::authorization]);

我为变量的类型写了auto,但它也可以是std::string。

现在,如果你想在你的请求中添加一个头,比如"授权",你可以把它写成另一个headers参数。下面是一个例子:

http_response response;
response.set_status_code(200);
response.headers().add(U("Authorization"), U("your token"));

希望它能帮助你和其他有类似问题的人。