Comments (4)
Hey @lganzzzo ,
Thanks it works correctly, because I have returned it inside the multipart through createDtoResponse
and I have had always a broken image. With the createResponse
, it works ;)
from oatpp-swagger.
Hello @remioukrat ,
What is the error message for your 500?
Also, you are using createDtoResponse
, and I assume that you have a json mapper set for your controller. So you are not returning a multipart but json instead.
To return a multipart response, you have to manually create multipart and set all of its parts.
You can use the following function as an example:
Create Multipart Function
#include "oatpp/web/mime/multipart/PartList.hpp"
#include "oatpp/web/protocol/http/outgoing/MultipartBody.hpp"
...
static std::shared_ptr<oatpp::web::mime::multipart::PartList>
createMultipart(const std::unordered_map<oatpp::String, oatpp::String>& map) {
auto multipart = oatpp::web::mime::multipart::PartList::createSharedWithRandomBoundary();
for(auto& pair : map) {
oatpp::web::mime::multipart::Headers partHeaders;
auto part = std::make_shared<oatpp::web::mime::multipart::Part>(partHeaders);
multipart->writeNextPartSimple(part);
part->putHeader("Content-Disposition", "form-data; name=\"" + pair.first + "\"");
part->setDataInfo(std::make_shared<oatpp::data::stream::BufferInputStream>(pair.second));
}
return multipart;
}
Send Multipart in the Response
ENDPOINT_ASYNC("GET", "/multipart", GetMultipart) {
ENDPOINT_ASYNC_INIT(GetMultipart)
Action act() override {
std::unordered_map<oatpp::String, oatpp::String> map;
map["part1"] = "data1";
map["part2"] = "part2";
auto responseBody = std::make_shared<oatpp::web::protocol::http::outgoing::MultipartBody>(createMultipart(map));
return _return(OutgoingResponse::createShared(Status::CODE_200, responseBody));
}
};
from oatpp-swagger.
@lganzzzo Thank you for the answer.
When I return the answer, I would like to return a file inside the swagger or an image using swagger image rendering. I understand that I return a json. But I would like to have the body in an attachment not mixed with headers.
part->putHeader("content-disposition", "attachment; filename=\"" + key + "\"");
part->putHeader("content-type", "image/jpeg;"
I have downloaded the response and I have removed the headers but the image can’t be displayed. I guess that I have made something wrong when I have filled my data like this
map["part"] = m_bufferStream->toString();
from oatpp-swagger.
Hey @remioukrat ,
I guess you don't need multipart to return an image.
Just return a binary
oatpp::String imgBuffer = getImageBuffer();
auto response = createResponse(Status::CODE_200, imgBuffer);
response->putHeader("content-type", "image/jpeg");
return response;
from oatpp-swagger.
Related Issues (20)
- Header parameters not shown in Swagger UI HOT 2
- missing cpack for module HOT 2
- Content-Type not set for CSS or JS responses HOT 2
- Error on start
- oatpp-swagger with HTTPS
- swagger-ui authorize bug
- To use "application/x-www-form-urlencoded" with readBodyDtoAsync HOT 1
- Set hosting base path
- Adding a Response with No Message Body Leads to an Oat++ Assertion Error at Runtime
- How To Describe File Type for `multipart/form-data` HOT 1
- The contact information cannot be an email. HOT 1
- bad interpretation of enum schemas
- Add authorization on the swagger controller
- Windows Support
- Crashed when added doc controller
- Access to raw OpenAPI JSON
- Problem with cmake FetchContent_* both oatpp-swagger and oatpp
- Support for schemas which better reflect DTO polymorphic fields
- [Help Wanted] how to group endpoints and give it a titile?
- Failed to load API definition HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from oatpp-swagger.