Skip to content

Example of an ArduinoJson request and response #25

@butaikis

Description

@butaikis

Hello. Please make an example of efficiently reading and sending large json data using the ArdinoJson 7 library. At the moment, I have implemented it this way.

Processing a request to the server:

WebServer.on(
      "/api", HTTP_POST, [this](AsyncWebServerRequest *request)
      {
      },
      NULL, [this](AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total)
      {
        
        if (CORE.busy != t_core::t_busy::FREE)
        {
          API_Busy(t_core::t_busy::HTTP, request);
          return;
        }
        debug("HTTP Req /api");
        CORE.busy = t_core::t_busy::HTTP;
        //read to buffer
        HTTP_SERVER_JSON_BUFFER.concat(data, len);


        debug("len: " + (String)len + " index: " + (String)index + " total: " + (String)total);

        if (index + len == total)
        {
          request->client()->setRxTimeout(10);
          TMR.http = millis();
          HTTP_Request = request;
          DeserializationError derr;
          if (derr = deserializeJson(INPUT_JSON, HTTP_SERVER_JSON_BUFFER))
          {
            // debug("Deserealiz ERR! " + (String ) derr.c_str());
            API_Send_Answer("error");
          }
          else
          {
            // debug("Deserealiz OK!");
            API_Read_JSON();
          }
        }
      });

Response processing:

//check size
 if (measureJson(OUT_JSON) > 61440)
 {
   API_Send_Answer("error_large_json_out");
   return;
 }
 
 AsyncResponseStream  *response = request->beginResponseStream("application/json", 61440);
 serializeJson(OUT_JSON, *response);
 request->send(response);

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions