Search code examples
http-status-codesstrapi

How and where to send status code for response in strapi?


I have developed simple posts endpoint and I do not know what to override and where to add status code. For example when some error happens I want to send 500 or when object created I want to send 201 instead of 200.

How and where to set status code in response of strapi?


Solution

  • Check out the strapi docs on responses, it uses a koa response object.
    So you can do something like:

    //controller
    ctx.send({
        message: 'The content was created!'
    }, 201);
    

    You can apply your own logic and response by using custom endpoints or modifying the existing ones. These are some closely related examples of how to do that - answer1 , answer2.

    Comment if you need more info