@@ -589,36 +589,37 @@ class Client {
589
589
std::shared_ptr<Response> Head (const char *path, const Headers &headers);
590
590
591
591
std::shared_ptr<Response> Post (const char *path, const std::string &body,
592
- const char *content_type);
592
+ const char *content_type, bool compress = false );
593
593
594
594
std::shared_ptr<Response> Post (const char *path, const Headers &headers,
595
595
const std::string &body,
596
- const char *content_type);
596
+ const char *content_type,
597
+ bool compress = false );
597
598
598
- std::shared_ptr<Response> Post (const char *path, const Params ¶ms);
599
+ std::shared_ptr<Response> Post (const char *path, const Params ¶ms, bool compress = false );
599
600
600
601
std::shared_ptr<Response> Post (const char *path, const Headers &headers,
601
- const Params ¶ms);
602
+ const Params ¶ms, bool compress = false );
602
603
603
604
std::shared_ptr<Response> Post (const char *path,
604
- const MultipartFormDataItems &items);
605
+ const MultipartFormDataItems &items, bool compress = false );
605
606
606
607
std::shared_ptr<Response> Post (const char *path, const Headers &headers,
607
- const MultipartFormDataItems &items);
608
+ const MultipartFormDataItems &items, bool compress = false );
608
609
609
610
std::shared_ptr<Response> Put (const char *path, const std::string &body,
610
- const char *content_type);
611
+ const char *content_type, bool compress = false );
611
612
612
613
std::shared_ptr<Response> Put (const char *path, const Headers &headers,
613
614
const std::string &body,
614
- const char *content_type);
615
+ const char *content_type, bool compress = false );
615
616
616
617
std::shared_ptr<Response> Patch (const char *path, const std::string &body,
617
- const char *content_type);
618
+ const char *content_type, bool compress = false );
618
619
619
620
std::shared_ptr<Response> Patch (const char *path, const Headers &headers,
620
621
const std::string &body,
621
- const char *content_type);
622
+ const char *content_type, bool compress = false );
622
623
623
624
std::shared_ptr<Response> Delete (const char *path);
624
625
@@ -3119,14 +3120,14 @@ inline std::shared_ptr<Response> Client::Head(const char *path,
3119
3120
3120
3121
inline std::shared_ptr<Response> Client::Post (const char *path,
3121
3122
const std::string &body,
3122
- const char *content_type) {
3123
- return Post (path, Headers (), body, content_type);
3123
+ const char *content_type, bool compress ) {
3124
+ return Post (path, Headers (), body, content_type, compress );
3124
3125
}
3125
3126
3126
3127
inline std::shared_ptr<Response> Client::Post (const char *path,
3127
3128
const Headers &headers,
3128
3129
const std::string &body,
3129
- const char *content_type) {
3130
+ const char *content_type, bool compress ) {
3130
3131
Request req;
3131
3132
req.method = " POST" ;
3132
3133
req.headers = headers;
@@ -3135,18 +3136,25 @@ inline std::shared_ptr<Response> Client::Post(const char *path,
3135
3136
req.headers .emplace (" Content-Type" , content_type);
3136
3137
req.body = body;
3137
3138
3139
+ if (compress) {
3140
+ if (!detail::compress (req.body )) {
3141
+ return nullptr ;
3142
+ }
3143
+ req.headers .emplace (" Content-Encoding" , " gzip" );
3144
+ }
3145
+
3138
3146
auto res = std::make_shared<Response>();
3139
3147
3140
3148
return send (req, *res) ? res : nullptr ;
3141
3149
}
3142
3150
3143
3151
inline std::shared_ptr<Response> Client::Post (const char *path,
3144
- const Params ¶ms) {
3145
- return Post (path, Headers (), params);
3152
+ const Params ¶ms, bool compress ) {
3153
+ return Post (path, Headers (), params, compress );
3146
3154
}
3147
3155
3148
3156
inline std::shared_ptr<Response>
3149
- Client::Post (const char *path, const Headers &headers, const Params ¶ms) {
3157
+ Client::Post (const char *path, const Headers &headers, const Params ¶ms, bool compress ) {
3150
3158
std::string query;
3151
3159
for (auto it = params.begin (); it != params.end (); ++it) {
3152
3160
if (it != params.begin ()) { query += " &" ; }
@@ -3155,58 +3163,53 @@ Client::Post(const char *path, const Headers &headers, const Params ¶ms) {
3155
3163
query += detail::encode_url (it->second );
3156
3164
}
3157
3165
3158
- return Post (path, headers, query, " application/x-www-form-urlencoded" );
3166
+ return Post (path, headers, query, " application/x-www-form-urlencoded" , compress );
3159
3167
}
3160
3168
3161
3169
inline std::shared_ptr<Response>
3162
- Client::Post (const char *path, const MultipartFormDataItems &items) {
3163
- return Post (path, Headers (), items);
3170
+ Client::Post (const char *path, const MultipartFormDataItems &items, bool compress ) {
3171
+ return Post (path, Headers (), items, compress );
3164
3172
}
3165
3173
3166
3174
inline std::shared_ptr<Response>
3167
3175
Client::Post (const char *path, const Headers &headers,
3168
- const MultipartFormDataItems &items) {
3169
- Request req;
3170
- req.method = " POST" ;
3171
- req.headers = headers;
3172
- req.path = path;
3173
-
3176
+ const MultipartFormDataItems &items, bool compress) {
3174
3177
auto boundary = detail::make_multipart_data_boundary ();
3175
3178
3176
- req.headers .emplace (" Content-Type" ,
3177
- " multipart/form-data; boundary=" + boundary);
3179
+ std::string body;
3178
3180
3179
3181
for (const auto &item : items) {
3180
- req. body += " --" + boundary + " \r\n " ;
3181
- req. body += " Content-Disposition: form-data; name=\" " + item.name + " \" " ;
3182
+ body += " --" + boundary + " \r\n " ;
3183
+ body += " Content-Disposition: form-data; name=\" " + item.name + " \" " ;
3182
3184
if (!item.filename .empty ()) {
3183
- req. body += " ; filename=\" " + item.filename + " \" " ;
3185
+ body += " ; filename=\" " + item.filename + " \" " ;
3184
3186
}
3185
- req. body += " \r\n " ;
3187
+ body += " \r\n " ;
3186
3188
if (!item.content_type .empty ()) {
3187
- req. body += " Content-Type: " + item.content_type + " \r\n " ;
3189
+ body += " Content-Type: " + item.content_type + " \r\n " ;
3188
3190
}
3189
- req. body += " \r\n " ;
3190
- req. body += item.content + " \r\n " ;
3191
+ body += " \r\n " ;
3192
+ body += item.content + " \r\n " ;
3191
3193
}
3192
3194
3193
- req. body += " --" + boundary + " --\r\n " ;
3195
+ body += " --" + boundary + " --\r\n " ;
3194
3196
3195
- auto res = std::make_shared<Response>();
3196
-
3197
- return send (req, *res) ? res : nullptr ;
3197
+ std::string content_type = " multipart/form-data; boundary=" + boundary;
3198
+ return Post (path, headers, body, content_type.c_str (), compress);
3198
3199
}
3199
3200
3200
3201
inline std::shared_ptr<Response> Client::Put (const char *path,
3201
3202
const std::string &body,
3202
- const char *content_type) {
3203
- return Put (path, Headers (), body, content_type);
3203
+ const char *content_type,
3204
+ bool compress) {
3205
+ return Put (path, Headers (), body, content_type, compress);
3204
3206
}
3205
3207
3206
3208
inline std::shared_ptr<Response> Client::Put (const char *path,
3207
3209
const Headers &headers,
3208
3210
const std::string &body,
3209
- const char *content_type) {
3211
+ const char *content_type,
3212
+ bool compress) {
3210
3213
Request req;
3211
3214
req.method = " PUT" ;
3212
3215
req.headers = headers;
@@ -3215,21 +3218,30 @@ inline std::shared_ptr<Response> Client::Put(const char *path,
3215
3218
req.headers .emplace (" Content-Type" , content_type);
3216
3219
req.body = body;
3217
3220
3221
+ if (compress) {
3222
+ if (!detail::compress (req.body )) {
3223
+ return nullptr ;
3224
+ }
3225
+ req.headers .emplace (" Content-Encoding" , " gzip" );
3226
+ }
3227
+
3218
3228
auto res = std::make_shared<Response>();
3219
3229
3220
3230
return send (req, *res) ? res : nullptr ;
3221
3231
}
3222
3232
3223
3233
inline std::shared_ptr<Response> Client::Patch (const char *path,
3224
3234
const std::string &body,
3225
- const char *content_type) {
3226
- return Patch (path, Headers (), body, content_type);
3235
+ const char *content_type,
3236
+ bool compress) {
3237
+ return Patch (path, Headers (), body, content_type, compress);
3227
3238
}
3228
3239
3229
3240
inline std::shared_ptr<Response> Client::Patch (const char *path,
3230
3241
const Headers &headers,
3231
3242
const std::string &body,
3232
- const char *content_type) {
3243
+ const char *content_type,
3244
+ bool compress) {
3233
3245
Request req;
3234
3246
req.method = " PATCH" ;
3235
3247
req.headers = headers;
@@ -3238,6 +3250,13 @@ inline std::shared_ptr<Response> Client::Patch(const char *path,
3238
3250
req.headers .emplace (" Content-Type" , content_type);
3239
3251
req.body = body;
3240
3252
3253
+ if (compress) {
3254
+ if (!detail::compress (req.body )) {
3255
+ return nullptr ;
3256
+ }
3257
+ req.headers .emplace (" Content-Encoding" , " gzip" );
3258
+ }
3259
+
3241
3260
auto res = std::make_shared<Response>();
3242
3261
3243
3262
return send (req, *res) ? res : nullptr ;
0 commit comments