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)
|
||||
|
||||
|
||||
@@ -8,14 +8,14 @@ int main() {
|
||||
WEBCC_LOG_INIT("", webcc::LOG_CONSOLE);
|
||||
|
||||
webcc::ClientSession session;
|
||||
session.Accept("application/json");
|
||||
|
||||
webcc::ResponsePtr r;
|
||||
|
||||
try {
|
||||
r = session.Send(webcc::RequestBuilder{}.
|
||||
Get("http://httpbin.org/get").
|
||||
Query("name", "Adam Gu", /*encode*/true).
|
||||
Header("Accept", "application/json").Date()
|
||||
Query("name", "Adam Gu", /*encode*/true).Date()
|
||||
());
|
||||
|
||||
assert(r->status() == webcc::Status::kOK);
|
||||
|
||||
@@ -13,6 +13,12 @@ ClientSession::ClientSession(int timeout, bool ssl_verify,
|
||||
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,
|
||||
const std::string& 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.
|
||||
// (https://www.zlib.net/zlib_faq.html#faq39)
|
||||
|
||||
headers_.Set(kAccept, "*/*");
|
||||
|
||||
#if WEBCC_ENABLE_GZIP
|
||||
headers_.Set(kAcceptEncoding, "gzip, deflate");
|
||||
#else
|
||||
headers_.Set(kAcceptEncoding, "identity");
|
||||
#endif // WEBCC_ENABLE_GZIP
|
||||
|
||||
headers_.Set(kAccept, "*/*");
|
||||
|
||||
headers_.Set(kConnection, "Keep-Alive");
|
||||
}
|
||||
|
||||
|
||||
@@ -46,6 +46,9 @@ public:
|
||||
charset_ = charset;
|
||||
}
|
||||
|
||||
// Set content types to accept.
|
||||
void Accept(const std::string& content_types);
|
||||
|
||||
// Set authorization.
|
||||
void Auth(const std::string& type, const std::string& credentials);
|
||||
|
||||
@@ -69,11 +72,11 @@ private:
|
||||
ResponsePtr DoSend(RequestPtr request, bool stream);
|
||||
|
||||
private:
|
||||
// Default media type for `Content-Type` header.
|
||||
// The media (or MIME) type of `Content-Type` header.
|
||||
// E.g., "application/json".
|
||||
std::string media_type_;
|
||||
|
||||
// Default charset for `Content-Type` header.
|
||||
// The charset of `Content-Type` header.
|
||||
// E.g., "utf-8".
|
||||
std::string charset_;
|
||||
|
||||
|
||||
@@ -22,6 +22,10 @@ RequestPtr RequestBuilder::operator()() {
|
||||
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 (!keep_alive_) {
|
||||
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));
|
||||
}
|
||||
|
||||
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,
|
||||
const std::string& credentials) {
|
||||
headers_.push_back(headers::kAuthorization);
|
||||
|
||||
+15
-7
@@ -117,6 +117,12 @@ public:
|
||||
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) {
|
||||
body_.reset(new StringBody{ data, false });
|
||||
return *this;
|
||||
@@ -148,11 +154,7 @@ public:
|
||||
RequestBuilder& FormData(const std::string& name, std::string&& data,
|
||||
const std::string& media_type = "");
|
||||
|
||||
RequestBuilder& Header(const std::string& key, const std::string& value) {
|
||||
headers_.push_back(key);
|
||||
headers_.push_back(value);
|
||||
return *this;
|
||||
}
|
||||
RequestBuilder& Header(const std::string& key, const std::string& value);
|
||||
|
||||
RequestBuilder& KeepAlive(bool keep_alive = true) {
|
||||
keep_alive_ = keep_alive;
|
||||
@@ -185,12 +187,18 @@ private:
|
||||
// Request 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_;
|
||||
|
||||
// Character set of the body (e.g., "utf-8").
|
||||
// The charset of `Content-Type` header.
|
||||
// E.g., "utf-8".
|
||||
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.
|
||||
std::vector<FormPartPtr> form_parts_;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user