How to send a big (>8Kb) json payload in a API_CALL

82 Views Asked by At

I am currently using OAT++ 1.3.0 to build a Client API. I declared a call API like: API_CALL("POST", "{svcPath}", postServiceOauth ,PATH(String, svcPath), HEADER(String, myHeader, "Content-Type"), AUTHORIZATION(String, authString, "Bearer"), BODY_STRING_JSON(String, body))

When calling postServiceOauth like :

postServiceOauth(svcRoute,header,accessToken,body);

with a body size >8Kb , this one is cut at exactly 8Kb.

So my question, how to send a JSON payload having a size > 8kB. Any example is welcome.

Tx for your help,


Here's the entire code used:

#define OATPP_MACRO_API_CLIENT_BODY_STRING_JSON(TYPE, PARAM_LIST) \
__body = oatpp::web::protocol::http::outgoing::BufferBody::createShared(OATPP_MACRO_FIRSTARG PARAM_LIST,"application/json");

#define BODY_STRING_JSON(TYPE, ...) OATPP_MACRO_API_CLIENT_PARAM(OATPP_MACRO_API_CLIENT_BODY_STRING_JSON, TYPE, (__VA_ARGS__))

class ClientApiCall : public oatpp::web::client::ApiClient {
public:

#include OATPP_CODEGEN_BEGIN(ApiClient)
 API_CALL("POST", "{svcPath}", postServiceChunkOauth ,PATH(String, svcPath),
         HEADER(String, myHeader, "Content-Type"),
         AUTHORIZATION(String, authString, "Bearer"),
         BODY_STRING_JSON(String, body))
#include OATPP_CODEGEN_END(ApiClient)
};

The call itself:

 auto objectMapper = oatpp::parser::json::mapping::ObjectMapper::createShared();
    auto requestExecutor = createCurlExecutor(BaseUrl.c_str());
    // auto requestExecutor = createOatppExecutor(BaseUrl.c_str());
    auto client = ClientApiCall::createShared(requestExecutor, objectMapper);

//some traces
 OATPP_LOGD("SVC CALL", "[%s] %s %d:(%s) getToken:%s", svcMethod.c_str(),svcRoute->c_str(),body->length(), body->c_str(),accessToken->c_str());

// call service
 auto svcresponse = client->postServiceOauth(svcRoute,header,accessToken,body);
 svcresponse->transferBody(&respCallback);

The respCallback is instance of:

class readResponse : public oatpp::data::stream::WriteCallback {
private:
  std::string str;
public:
  std::string& getResponse() {
    return str;
  }


  oatpp::v_io_size write(const void *data, v_buff_size count, oatpp::async::Action& action) {
    
    str.append((char*)data,count);
    fprintf(stdout," data(%d)%d='%s' \n",str.length(), count,str.c_str());
    OATPP_LOGD("callback",  "[write] %d",str.length());
    return (oatpp::v_io_size)str.length();
  }
};

so by execution i've got the following logs:

SVC CALL:[post] /v2/deletion-statuses 57745:([{ "delete_appli.........

57745 is the size of the body (body->length())

The log of the readResponse callback is: callback:[write] 215 and the response message is: {"code":400,"errors":[{"message":"Parsing error for a JSON content type: Unexpected character '}' (Codepoint: 125) on [lineNumber=1, columnNumber=8193, streamOffset=8192]. Reason is [[End of file hit too early]]"}]}'

1

There are 1 best solutions below

0
Christophe H On

So I rebuilt my call using oatpp-libressl module, and now it works. So it seems that the limit was due to a (maybe bad) usage of oatpp-curl. Herewith are some tips.

  • When you need to call an identity provider, you have to remove the https from the URL and the first / from the identity provider service name.

  • I defined the macro

    #define BODY_STRING_JSON(TYPE, ...) OATPP_MACRO_API_CLIENT_PARAM(OATPP_MACRO_API_CLIENT_BODY_STRING_JSON, TYPE, (VA_ARGS))

and used it :

 API_CALL("POST", "{svcPath}", postServiceOauth ,PATH(String, svcPath),
        HEADER(String, myHeader, "Content-Type"),
        AUTHORIZATION(String, authString, "Bearer"),
        BODY_STRING_JSON(String, body))

to be able to send a JSON. Without this, the server gave back an error 400.

that's all, Kind regards, Christophe