Oat++ response method readBodyToDto example

707 Views Asked by At

Has anybody used objectMapper to parse the body of a oatpp::web::client::RequestExecutir::Response using method readBodyToDto. I am building a REST API client and I haven't been able to map the returned object into my dto. Here's the piece of code that I am struggling with:

void GoRestApi::getUser(const std::shared_ptr<GoRestApiClient>& client)
{
   auto response = client->doGetUser(2600);
   oatpp::String contentType = response->getHeader("Content-Type");
// auto userDto = response->readBodyToDto<oatpp::Object<UserDto>>(objectMapper);
   auto data = response->readBodyToString();

   OATPP_LOGD(TAG, "[doGetUser] data='%s'", data->c_str());
}

I commented out the line that I can't get even to compile. If you have some code snippet that you can share, I'd love it. Thanks a bundle.

2

There are 2 best solutions below

1
Mike On

This works fine for me

    auto objectMapper = oatpp::parser::json::mapping::ObjectMapper::createShared();
    auto dtmResponseDTO = response->readBodyToDto<oatpp::Object<DtmResponseDTO>>(objectMapper);
0
A.BEN On

May be you should import or initialize the objectMapper to compile. I got another issue when deserializing too, when it seems to be blocked on readBodyToDto

I also tried readFromString

auto objectMapper = oatpp::parser::json::mapping::ObjectMapper::createShared();

auto reponseIdDto = objectMapper->readFromString<oatpp::Object<ReponseIdDto>>(response->readBodyToString());