Browse Source

Catch wrong datatype on POST to REST-API (/api/ctrl)

pull/1452/head
Stefan Pokorny 1 year ago
parent
commit
544b6666fc
  1. 15
      src/web/RestApi.h
  2. 6
      src/web/lang.h

15
src/web/RestApi.h

@ -63,6 +63,7 @@ class RestApi {
void ctrlRequest(JsonObject obj) {
DynamicJsonDocument json(128);
JsonObject dummy = json.as<JsonObject>();
if(obj[F("path")] == "ctrl")
setCtrl(obj, dummy, "*");
else if(obj[F("path")] == "setup")
@ -155,12 +156,14 @@ class RestApi {
DynamicJsonDocument json(1000);
DeserializationError err = deserializeJson(json, reinterpret_cast<const char*>(mTmpBuf), mTmpSize);
JsonObject obj = json.as<JsonObject>();
AsyncJsonResponse* response = new AsyncJsonResponse(false, 200);
JsonObject root = response->getRoot();
if (json.is<JsonObject>()) {
JsonObject obj = json.as<JsonObject>();
root[F("success")] = (err) ? false : true;
if(!err) {
String path = request->url().substring(5);
if(path == "ctrl")
root[F("success")] = setCtrl(obj, root, request->client()->remoteIP().toString().c_str());
@ -170,16 +173,18 @@ class RestApi {
root[F("success")] = false;
root[F("error")] = F(PATH_NOT_FOUND) + path;
}
} else {
switch (err.code()) {
case DeserializationError::Ok: break;
case DeserializationError::Ok: root[F("error")] = F(INVALID_DATATYPE); break;
case DeserializationError::IncompleteInput: root[F("error")] = F(INCOMPLETE_INPUT); break;
case DeserializationError::InvalidInput: root[F("error")] = F(INVALID_INPUT); break;
case DeserializationError::NoMemory: root[F("error")] = F(NOT_ENOUGH_MEM); break;
default: root[F("error")] = F(DESER_FAILED); break;
}
}
}
response->setLength();
request->send(response);
delete[] mTmpBuf;

6
src/web/lang.h

@ -42,6 +42,12 @@
#define INVALID_INPUT "Invalid input"
#endif
#ifdef LANG_DE
#define INVALID_DATATYPE "Ungültiger Datentyp"
#else /*LANG_EN*/
#define INVALID_DATATYPE "Invalid datatype"
#endif
#ifdef LANG_DE
#define NOT_ENOUGH_MEM "nicht genügend Speicher"
#else /*LANG_EN*/

Loading…
Cancel
Save