scalahttptapir

Tapir custom http code in successful response


I'm using Tapir library for endpoint definition but I need to provide custom http code in a successful response (not to map error to error code). I've found something in the documentation but it's not really useful to me. Could somebody give me simple code example?


Solution

  • If you want to always provide a given status code in case of a successful response, adding the following output should work:

    import sttp.model.StatusCode
    
    myEndpoint.out(statusCode(StatusCodes.Created))
    

    Alternatively, you can add a dynamic status code output:

    import sttp.model.StatusCode
    
    myEndpoint.out(statusCode)
    

    Then you'll need to provide the StatusCode value as part of the business logic.