boost http響應讀取

weixin_33843409發表於2018-04-17

響應報樣例:

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裡這樣定義:

深入看下去,meaage繼承自:

從上面的紅色測試程式碼可看出,可以直接獲取包頭的一些資訊,但是如果是dynamic_body的話不能用直接通過body()獲取,還沒研究出用法,知道其涉及到const_buffers_type、multi_buffer等相關類

 

趕專案,先不研究,有空了再研究

相關文章