天天看点

boost http响应读取

响应报样例:

HTTP/1.1 201 Created

Content-Type: application/json

X-Etcd-Cluster-Id: cdf818194e3a8c32

X-Etcd-Index: 115

X-Raft-Index: 1146

X-Raft-Term: 3

Date: Tue, 17 Apr 2018 07:36:20 GMT

Content-Length: 171

{"action":"set","node":{"key":"/test/node/node_using","value":"10.20.6.99","expiration":"2018-04-17T07:36:30.763332719Z","ttl":10,"modifiedIndex":115,"createdIndex":115}}

部分代码:

http::response<http::string_body> res;

http::read(socket, buffer, res);

cout << "Result:" << res.result() << "." << res.result_int() << endl; 

cout << "Contentlen:" << res.body().size() << endl; 

cout << "has len:" << res.has_content_length() << endl; 

cout << "body:" << res.body() << endl;

输出:

Result:Created.201

Contentlen:171

has len:1

一开始用dynamic_body,被坑的好惨,dynamic_body还不清楚原理,想通过res.body()获取包体死活不行,改为string_body直接搞定

研究了半天大概了解了下boost/beast/http/message.hpp里这样定义:

boost http响应读取

深入看下去,meaage继承自:

boost http响应读取

从上面的红色测试代码可看出,可以直接获取包头的一些信息,但是如果是dynamic_body的话不能用直接通过body()获取,还没研究出用法,知道其涉及到const_buffers_type、multi_buffer等相关类

赶项目,先不研究,有空了再研究