Skip to content

Commit

Permalink
Merge pull request #12 from HashLoad/Valid-JSON
Browse files Browse the repository at this point in the history
Valid JSON
  • Loading branch information
viniciussanchez authored Jan 28, 2022
2 parents a62e902 + d66b79b commit 0b28043
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/Horse.Jhonson.pas
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,24 @@ procedure Middleware(Req: THorseRequest; Res: THorseResponse; Next: {$IF DEFINED
var
LJSON: {$IF DEFINED(FPC)}TJsonData{$ELSE}TJSONValue{$ENDIF};
begin
if ({$IF DEFINED(FPC)} StringCommandToMethodType(Req.RawWebRequest.Method)
{$ELSE} Req.RawWebRequest.MethodType{$ENDIF} in [mtPost, mtPut, mtPatch]) and (Req.RawWebRequest.ContentType = 'application/json') then
if (Req.MethodType in [mtPost, mtPut, mtPatch]) and (Req.RawWebRequest.ContentType = 'application/json') then
begin
LJSON := {$IF DEFINED(FPC)} GetJSON(Req.Body) {$ELSE}TJSONObject.ParseJSONValue(Req.Body){$ENDIF};
try
LJSON := {$IF DEFINED(FPC)} GetJSON(Req.Body) {$ELSE}TJSONObject.ParseJSONValue(Req.Body){$ENDIF};
except
Res.Send('Invalid JSON').Status(THTTPStatus.BadRequest);
raise EHorseCallbackInterrupted.Create;
end;

if not Assigned(LJSON) then
begin
Res.Send('Invalid JSON').Status(THTTPStatus.BadRequest);
raise EHorseCallbackInterrupted.Create;
end;

Req.Body(LJSON);
end;

try
Next;
finally
Expand Down

0 comments on commit 0b28043

Please sign in to comment.