Skip to content

Commit d910bfc

Browse files
authored
Merge pull request #279 from yhirose/multipart
Content receiver support for multipart content (Fix #241)
2 parents 5e37e38 + b69c0a1 commit d910bfc

File tree

3 files changed

+454
-186
lines changed

3 files changed

+454
-186
lines changed

README.md

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ svr.Post("/multipart", [&](const auto& req, auto& res) {
9090
const auto& file = req.get_file_value("name1");
9191
// file.filename;
9292
// file.content_type;
93-
auto body = req.body.substr(file.offset, file.length);
93+
// file.content;
9494
});
9595

9696
```
@@ -118,12 +118,26 @@ svr.Get("/stream", [&](const Request &req, Response &res) {
118118
```cpp
119119
svr.Post("/content_receiver",
120120
[&](const Request &req, Response &res, const ContentReader &content_reader) {
121-
std::string body;
122-
content_reader([&](const char *data, size_t data_length) {
123-
body.append(data, data_length);
124-
return true;
125-
});
126-
res.set_content(body, "text/plain");
121+
if (req.is_multipart_form_data()) {
122+
MultipartFiles files;
123+
content_reader(
124+
[&](const std::string &name, const char *data, size_t data_length) {
125+
auto &file = files.find(name)->second;
126+
file.content.append(data, data_length);
127+
return true;
128+
},
129+
[&](const std::string &name, const MultipartFile &file) {
130+
files.emplace(name, file);
131+
return true;
132+
});
133+
} else {
134+
std::string body;
135+
content_reader([&](const char *data, size_t data_length) {
136+
body.append(data, data_length);
137+
return true;
138+
});
139+
res.set_content(body, "text/plain");
140+
}
127141
});
128142
```
129143

0 commit comments

Comments
 (0)