add Accept() for Accept header to request builder
This commit is contained in:
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
cmake_minimum_required(VERSION 3.1.0)
|
cmake_minimum_required(VERSION 3.11)
|
||||||
|
|
||||||
project(webcc)
|
project(webcc)
|
||||||
|
|
||||||
|
|||||||
@@ -8,14 +8,14 @@ int main() {
|
|||||||
WEBCC_LOG_INIT("", webcc::LOG_CONSOLE);
|
WEBCC_LOG_INIT("", webcc::LOG_CONSOLE);
|
||||||
|
|
||||||
webcc::ClientSession session;
|
webcc::ClientSession session;
|
||||||
|
session.Accept("application/json");
|
||||||
|
|
||||||
webcc::ResponsePtr r;
|
webcc::ResponsePtr r;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
r = session.Send(webcc::RequestBuilder{}.
|
r = session.Send(webcc::RequestBuilder{}.
|
||||||
Get("http://httpbin.org/get").
|
Get("http://httpbin.org/get").
|
||||||
Query("name", "Adam Gu", /*encode*/true).
|
Query("name", "Adam Gu", /*encode*/true).Date()
|
||||||
Header("Accept", "application/json").Date()
|
|
||||||
());
|
());
|
||||||
|
|
||||||
assert(r->status() == webcc::Status::kOK);
|
assert(r->status() == webcc::Status::kOK);
|
||||||
|
|||||||
@@ -13,6 +13,12 @@ ClientSession::ClientSession(int timeout, bool ssl_verify,
|
|||||||
InitHeaders();
|
InitHeaders();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ClientSession::Accept(const std::string& content_types) {
|
||||||
|
if (!content_types.empty()) {
|
||||||
|
headers_.Set(headers::kAccept, content_types);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void ClientSession::Auth(const std::string& type,
|
void ClientSession::Auth(const std::string& type,
|
||||||
const std::string& credentials) {
|
const std::string& credentials) {
|
||||||
headers_.Set(headers::kAuthorization, type + " " + credentials);
|
headers_.Set(headers::kAuthorization, type + " " + credentials);
|
||||||
@@ -74,14 +80,14 @@ void ClientSession::InitHeaders() {
|
|||||||
// Simply put, "deflate" is not recommended for HTTP 1.1 encoding.
|
// Simply put, "deflate" is not recommended for HTTP 1.1 encoding.
|
||||||
// (https://www.zlib.net/zlib_faq.html#faq39)
|
// (https://www.zlib.net/zlib_faq.html#faq39)
|
||||||
|
|
||||||
|
headers_.Set(kAccept, "*/*");
|
||||||
|
|
||||||
#if WEBCC_ENABLE_GZIP
|
#if WEBCC_ENABLE_GZIP
|
||||||
headers_.Set(kAcceptEncoding, "gzip, deflate");
|
headers_.Set(kAcceptEncoding, "gzip, deflate");
|
||||||
#else
|
#else
|
||||||
headers_.Set(kAcceptEncoding, "identity");
|
headers_.Set(kAcceptEncoding, "identity");
|
||||||
#endif // WEBCC_ENABLE_GZIP
|
#endif // WEBCC_ENABLE_GZIP
|
||||||
|
|
||||||
headers_.Set(kAccept, "*/*");
|
|
||||||
|
|
||||||
headers_.Set(kConnection, "Keep-Alive");
|
headers_.Set(kConnection, "Keep-Alive");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -46,6 +46,9 @@ public:
|
|||||||
charset_ = charset;
|
charset_ = charset;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set content types to accept.
|
||||||
|
void Accept(const std::string& content_types);
|
||||||
|
|
||||||
// Set authorization.
|
// Set authorization.
|
||||||
void Auth(const std::string& type, const std::string& credentials);
|
void Auth(const std::string& type, const std::string& credentials);
|
||||||
|
|
||||||
@@ -69,11 +72,11 @@ private:
|
|||||||
ResponsePtr DoSend(RequestPtr request, bool stream);
|
ResponsePtr DoSend(RequestPtr request, bool stream);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Default media type for `Content-Type` header.
|
// The media (or MIME) type of `Content-Type` header.
|
||||||
// E.g., "application/json".
|
// E.g., "application/json".
|
||||||
std::string media_type_;
|
std::string media_type_;
|
||||||
|
|
||||||
// Default charset for `Content-Type` header.
|
// The charset of `Content-Type` header.
|
||||||
// E.g., "utf-8".
|
// E.g., "utf-8".
|
||||||
std::string charset_;
|
std::string charset_;
|
||||||
|
|
||||||
|
|||||||
@@ -22,6 +22,10 @@ RequestPtr RequestBuilder::operator()() {
|
|||||||
request->SetHeader(std::move(headers_[i - 1]), std::move(headers_[i]));
|
request->SetHeader(std::move(headers_[i - 1]), std::move(headers_[i]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!accept_.empty()) {
|
||||||
|
request->SetHeader(headers::kAccept, std::move(accept_));
|
||||||
|
}
|
||||||
|
|
||||||
// If no Keep-Alive, explicitly set `Connection` to "Close".
|
// If no Keep-Alive, explicitly set `Connection` to "Close".
|
||||||
if (!keep_alive_) {
|
if (!keep_alive_) {
|
||||||
request->SetHeader(headers::kConnection, "Close");
|
request->SetHeader(headers::kConnection, "Close");
|
||||||
@@ -78,6 +82,13 @@ RequestBuilder& RequestBuilder::FormData(const std::string& name,
|
|||||||
return Form(FormPart::New(name, std::move(data), media_type));
|
return Form(FormPart::New(name, std::move(data), media_type));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
RequestBuilder& RequestBuilder::Header(const std::string& key,
|
||||||
|
const std::string& value) {
|
||||||
|
headers_.push_back(key);
|
||||||
|
headers_.push_back(value);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
RequestBuilder& RequestBuilder::Auth(const std::string& type,
|
RequestBuilder& RequestBuilder::Auth(const std::string& type,
|
||||||
const std::string& credentials) {
|
const std::string& credentials) {
|
||||||
headers_.push_back(headers::kAuthorization);
|
headers_.push_back(headers::kAuthorization);
|
||||||
|
|||||||
+15
-7
@@ -117,6 +117,12 @@ public:
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set content types to accept.
|
||||||
|
RequestBuilder& Accept(const std::string& content_types) {
|
||||||
|
accept_ = content_types;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
RequestBuilder& Body(const std::string& data) {
|
RequestBuilder& Body(const std::string& data) {
|
||||||
body_.reset(new StringBody{ data, false });
|
body_.reset(new StringBody{ data, false });
|
||||||
return *this;
|
return *this;
|
||||||
@@ -148,11 +154,7 @@ public:
|
|||||||
RequestBuilder& FormData(const std::string& name, std::string&& data,
|
RequestBuilder& FormData(const std::string& name, std::string&& data,
|
||||||
const std::string& media_type = "");
|
const std::string& media_type = "");
|
||||||
|
|
||||||
RequestBuilder& Header(const std::string& key, const std::string& value) {
|
RequestBuilder& Header(const std::string& key, const std::string& value);
|
||||||
headers_.push_back(key);
|
|
||||||
headers_.push_back(value);
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
RequestBuilder& KeepAlive(bool keep_alive = true) {
|
RequestBuilder& KeepAlive(bool keep_alive = true) {
|
||||||
keep_alive_ = keep_alive;
|
keep_alive_ = keep_alive;
|
||||||
@@ -185,12 +187,18 @@ private:
|
|||||||
// Request body.
|
// Request body.
|
||||||
BodyPtr body_;
|
BodyPtr body_;
|
||||||
|
|
||||||
// Media type of the body (e.g., "application/json").
|
// The media (or MIME) type of `Content-Type` header.
|
||||||
|
// E.g., "application/json".
|
||||||
std::string media_type_;
|
std::string media_type_;
|
||||||
|
|
||||||
// Character set of the body (e.g., "utf-8").
|
// The charset of `Content-Type` header.
|
||||||
|
// E.g., "utf-8".
|
||||||
std::string charset_;
|
std::string charset_;
|
||||||
|
|
||||||
|
// Accept content types (could be comma separated multiple types).
|
||||||
|
// E.g., "application/json", "text/html, application/xhtml+xml".
|
||||||
|
std::string accept_;
|
||||||
|
|
||||||
// Files to upload for a POST request.
|
// Files to upload for a POST request.
|
||||||
std::vector<FormPartPtr> form_parts_;
|
std::vector<FormPartPtr> form_parts_;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user