Detect wrong parameter with REST

Home Forums kbmMW Detect wrong parameter with REST

  • This topic has 1 reply, 2 voices, and was last updated 2 months ago by kimbomadsen.
Viewing 1 reply thread
  • Author
    Posts
    • #54207
      mrluigi2017
      Participant

      Hi,

      I would like to return a different http statuscode when a wrong parameter is passed to a REST method. If I am not mistaken an internal server error occurs by default. I know I can use:

      kbmMWRaiseHTTPError(407,’….’) or

      raise EkbmMWHTTPException.Create(407,’…’);

      But how do I know that the (rest body) parameter is wrong?

      Thank in advance

      Luigi

    • #54215
      kimbomadsen
      Keymaster

      If its data extracted from the URL path part {..} syntax, or data extracted from the query part $… syntax, then just define the arguments to be required in the arguments REST attribute [kbmMW_Rest(‘value:”{value2}”, required:true’)]

      If its data attempted to be extracted from the body part, then all you refer to is [kbmMW_Rest(‘value:body’)]’. A body may or may not contain anything. You however later on use TkbmMWHTTPQueryValues to extract the body parts.

      var
      qv:TkbmMWHTTPQueryValues; // Found in kbmMWHTTPUtils.pas
      begin
        qv:=TkbmMWHTTPQueryValues.Create;
        try
         qv.AsString:=ABody; // If the body is URL encoded you can use qv.AsEncodedString:=ABody
         value1:=qv.ValueByName[‘value1’];
         value2:=qv.ValueByName[‘value2’];
        finally
          qv.Free;
        end;
      end;
      You can at any place check for the values existence and if it does not exist, throw an exception.
Viewing 1 reply thread
  • You must be logged in to reply to this topic.
Skip to toolbar