# Header Input validation returns text/plain instead of application/JSON

**URL:** <https://softwaremill.community/t/header-input-validation-returns-text-plain-instead-of-application-json/184>\
**Category:** tapir\
**Created:** [April 11, 2023, 10:48pm UTC](https://softwaremill.community/t/header-input-validation-returns-text-plain-instead-of-application-json/184 "2023-04-11T22:48:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![frozenwizard](https://avatars.discourse-cdn.com/v4/letter/f/f4b2a3/32.png) [@frozenwizard](https://softwaremill.community/u/frozenwizard)\
**Post date:** [April 11, 2023, 10:48pm UTC](https://softwaremill.community/t/header-input-validation-returns-text-plain-instead-of-application-json/184/1 "2023-04-11T22:48:01Z")

</div>

So it’s not obvious to me but I have some Header inputs that I use validate on to make sure it’s not empty when I map it to an Authentication token

```plaintext
header[String]("foo").validate(Validator.nonEmptyString)

```

Short version: I want to map validation errors to application/json and set the status code.

Longer version:

There’s an issue when I generate the OpenApi doc where it converts it to a 400 and text/plain with the error message. I would prefer this to be in application/Json

Using openapi-generator it doesn’t generate the correct handling for this anyway. The client expects text/plain but the Tapir server generates “text/plain; charset=UTF8” (or something to the effect) and the generated client blows up parsing this as a mismatch.

Working around this I was able to set the defaultHandlers

```plaintext
case class Unknown(msg: String)
def myFailureResponse(m: String): ValuedEndpointOutput[_] = ValuedEndpointOutput(jsonBody[Unknown], Unknown(m))

val serverOptions: Http4sServerOptions[IO] = Http4sServerOptions.customiseInterceptors[IO].defaultHandlers(myFailureResponse).metricsInterceptor(prometheusMetrics.metricsInterceptor()).options

```

This does force it to return application/json and the Json message I would want, but the generated OpenApi doc still specifies this as text/plain, so the client blows up because the json returned is not text/plain.

---

<div class="post-metadata">

**Author:** ![kciesielski](https://dub1.discourse-cdn.com/flex005/user_avatar/softwaremill.community/kciesielski/32/133_2.png) [@kciesielski](https://softwaremill.community/u/kciesielski)\
**Post date:** [April 12, 2023, 2:46pm UTC](https://softwaremill.community/t/header-input-validation-returns-text-plain-instead-of-application-json/184/2 "2023-04-12T14:46:25Z")

</div>

You can specify error types using `.errorOut`, for example:

```plaintext
sealed trait ApiError

case class BadRequestError(code: Int, description: String) extends ApiError
case object UnauthorizedErr extends ApiError

// ...

sealed trait ApiError

case class BadRequestError(code: Int, description: String) extends ApiError
case object UnauthorizedErr extends ApiError

  val booksListing: Endpoint[Unit, String, ApiError, List[Book], Any] =
    endpoint.get
      .in("books" / "list" / "all")
      .in(header[String]("foo").validate(Validator.nonEmptyString))
      .out(jsonBody[List[Book]])
      .errorOut(
        oneOf[ApiError](
          oneOfVariant(statusCode(StatusCode.BadRequest).and(jsonBody[BadRequestError])),
          oneOfVariant(statusCode(StatusCode.Unauthorized).and(emptyOutputAs(UnauthorizedErr)))
        )
      )  

```

This will generate `OpenApi` specification with 400s represented with the `BadRequestError` schema and `application/json` as content type.  
(Still, the interceptor part is required to return the actual JSON on failed header validation).

---

<div class="post-metadata">

**Author:** ![frozenwizard](https://avatars.discourse-cdn.com/v4/letter/f/f4b2a3/32.png) [@frozenwizard](https://softwaremill.community/u/frozenwizard)\
**Post date:** [April 13, 2023, 4:28pm UTC](https://softwaremill.community/t/header-input-validation-returns-text-plain-instead-of-application-json/184/3 "2023-04-13T16:28:24Z")

</div>

Ok cool that worked thanks for your help. The docs weren’t clear on this, when I get a moment I’ll create a PR to mention this in the docs.

As per the open api generated mismatch of text/plain and “text/plain; charset=UTF8” is that a defect that needs to be opened?

---

<div class="post-metadata">

**Author:** ![kciesielski](https://dub1.discourse-cdn.com/flex005/user_avatar/softwaremill.community/kciesielski/32/133_2.png) [@kciesielski](https://softwaremill.community/u/kciesielski)\
**Post date:** [April 14, 2023, 7:12pm UTC](https://softwaremill.community/t/header-input-validation-returns-text-plain-instead-of-application-json/184/4 "2023-04-14T19:12:29Z")

</div>

I think it’s a defect of the `openapi-generator`. In this case, the server is indicating that the response is of type `"text/plain"` and also providing information about the character encoding used in the response by specifying `"charset=UTF-8"` in the `"content-type"` header. This is actually a good practice because it allows the client to correctly interpret the response.
