REST HTTP StatusCode without raising an error

Home Forums kbmMW REST HTTP StatusCode without raising an error

Viewing 4 reply threads
  • Author
    Posts
    • #56775
      mrluigi2017
      Participant

      Hi,

      I want to control what http status code is returned to the client. I know you can do this with

      kbmMWRaiseHTTPError(407,’….’)

      or

      raise EkbmMWHTTPException.Create(407,’…’)

      But it seems a bit odd to use an error if it isn’t for instance if you want to return a 201 Create status code.

      I have tried to set the status code as in the code sample, but it doesn’t seem to work (I still get a 200 status code)

      var
      hlp: TkbmMWHTTPTransportStreamHelper;
      begin
      hlp := TkbmMWHTTPTransportStreamHelper(ResponseTransportStream.Helper);
      hlp.HTTPStatusCode := 201;
      // kbmMWRaiseHTTPError(201, ”);
      Result := ”;
      end;

      Any suggestions?

      • This topic was modified 3 years, 1 month ago by mrluigi2017.
    • #56782
      kimbomadsen
      Keymaster

      Hi,

      You can do:

      ResponseTransportStream.Params.Values[KBMMW_HTTP_PARAM_OK_STATUS_CODE]:=’201′;

      /Kim

    • #56783
      mrluigi2017
      Participant

      In Fiddler and Postman I still get a 200 status code instead of 201. Are you sure that this should work?

    • #56784
      mrluigi2017
      Participant

      It looks like that in unit kbmMWHTTPSysServerTransport (didn’t check other transports yet) on line 579 the status code that is set is overwritten by this line of code:

      reshlp.HTTPStatusCode:=reshlp.HTTPStatusCodeFromServerStatusCode(res.StatusCode);

       

      • #56785
        kimbomadsen
        Keymaster

        Hi,

        Ok.. I assumed you were talking about our native transports and REST.

        /Kim

    • #56786
      mrluigi2017
      Participant

      Sorry that 🙁 I am using kbmMWHTTPSysServerTransport. I have tested it with kbmMWTCPServerTransport and kbmMWTCPIPIndyServerTransport and that seems to works fine.

      • #56813
        kimbomadsen
        Keymaster

        Hi,

        You should be able to do the following in the method that handles your request in the server:

        ResponseStream.Error(KBMMW_ERR_HTTP_EXCEPTIONS_MIN+310,’Returning HTTP status 310′); // This will not raise an exception.

        Or if you want to signal an error and thus want to signal a specific error code, but not return data. It will actually raise an exception.

        kbmMWRaiseHTTPError(310.’Returning HTTP status 310′);

        /Kim

Viewing 4 reply threads
  • You must be logged in to reply to this topic.