Giter Club home page Giter Club logo

Comments (4)

remioukrat avatar remioukrat commented on June 16, 2024 1

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.

lganzzzo avatar lganzzzo commented on June 16, 2024

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.

remioukrat avatar remioukrat commented on June 16, 2024

@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.

lganzzzo avatar lganzzzo commented on June 16, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.