Set status code to 201 in response

Hi all,

I’m wondering how I can set status code to 201 for response. I dive into documentation and i didn’t found the way to achieve it.

I would like answer by Htp 201 with JsonBody payload. I sppose there is an other way than oneOfVariant ?

Many thanks for your help,

Kind regards,

You can use the statusCode output (see also the docs). There are two variants - either a static status code (statusCode(StatusCode.Created)). Or a dynamic one, which is set by the server logic (statusCode without parameters).

Hi!

Many thanks for your answer. I wish you happy new year :hugs:.

From my comprehension, in case of using statusCode(…) that means that I can’t put nothing in response body?

Also, by using onOf Variant I must used with error canal and in my case created status code is not error case (if I am not’wrong)

You know what I mean?

May be do you have one example?

You can combine the output with any other, e.g. statusCode(Created).and(stringBody). Or alternatively: endpoint.out(statusCode).out(stringBody).

I’ve created a small example here: tapir/StatusCodeNettyFutureServer.scala at master · softwaremill/tapir · GitHub

Hi Adam,

Many thanks for your help. It is clearer now. With your helps, I achieve what I wanted to do.

Kind regards

1 Like