Separated endpoints based on header is present or not

hi,
how to define endpoint that will return 403 for all PUT request not containing specific header with name game-id? I want to also define endpoints that will accept PUT request in case header is present.
current API does not seems to support chatgpt’s suggestion

.when(gameIdHeader.notPresent)

:slight_smile:
thank you
regards,
peter

Hey,

I’m not sure if chat-gpt’s cutoff date allows it to include much information on tapir :slight_smile:

Anyway, to implement such an endpoint, I think the simplest solution would be sth as follows:

val myServerEndpoint = endpoint.put
  .in(header[Option[String]]("game-id"))
  .errorOut(statusCode)
  .out(stringBody)
  .serverLogic(header =>
    header match {
      case None => Future.successfull(Left(StatusCodes.Forbidden))
      case Some(v) => Future.successfull(Right("ok"))
    }
  )

Of course you’ll probably want to do more in the endpoint.

Alternatively, if you’d like to mark the header as required in the docs, you can use a required endpoint: header[String]("game-id") and modify the response, in case the endpoint fails to decode, using a custom decode failure handler.