How to return HTTP Status Codes from API?

I want to return proper status codes if something went wrong, e.g. 404 if a resource is not found or 403 if the permission is not sufficient.

In .net web api, one can express that with the return type. If you use IHttpActionResult or HttpResponseMessage you can modify the headers beside the content.
So I wonder if there's a similar method to return the status code beside the content in the API designer?

I tried it with HttpResponseMessage but had no luck (it returns the object as JSON with status code 200).

Parents Reply Children
  • No, didn't stumbled upon that. Sorry and thanks.

    That example uses a HttpException to return status codes. While that works fine in some (if not most) cases, it assumes that every status code change has to be an error.
    That's not true for status codes 1xx-3xx.

    Indeed, for most of my work this is sufficient.

  • Hi Wolfgang,

    You could emit any status code without using a HttpException, but it's a bit more complex.

    It's true that the default model focuses on the data, and abstracts away stuff like HTTP headers, status codes and content types. There will be a more flexible way to configure the underlying HttpResponseMessage object starting with version 8.2.

    Do you have a specific use case for the status codes 1xx-3xx in mind?

    Hanno

  • Hi Hanno,

    we had 201 (created), 202 (accepted - e.g. on creating a process), 204 (no content), and 304 (not modified) in mind when we started to investigate. And as I wrote - for most of our work we could forego different status codes.

    But 8.2 would be fine for us - thanks.