Skip to content

Context.body() throws error if charset parameter is quoted #2448

@Pinusar

Description

@Pinusar

Actual behavior (the bug)
if Content-type header charset is in quotes, Javalin throws an error when accessing Context.body()

WARN io.javalin.Javalin - Uncaught exception
java.nio.charset.IllegalCharsetNameException: "utf8"

Expected behavior
Request body should be accessed normally, as if it was without quotes.

To Reproduce

@Test
    fun `quoted charset encoding should be handled successfully`() {
        TestUtil.test { app, http ->
            app.post("/foo") { ctx ->
                ctx.result(ctx.body())
            }
            val result = http.post("/foo")
                .header("Content-type", "application/json;charset=\"utf8\"")
                .body("{}")
                .asString()
            assertThat(result.status).isEqualTo(200)
        }
    }

Additional context
According to https://datatracker.ietf.org/doc/html/rfc7231

A parameter value that matches the token production can be
   transmitted either as a token or within a quoted-string.  The quoted
   and unquoted values are equivalent.  For example, the following
   examples are all equivalent, but the first is preferred for
   consistency:

     text/html;charset=utf-8
     text/html;charset=UTF-8
     Text/HTML;Charset="utf-8"
     text/html; charset="utf-8"

Some clients send the charset in quotes.

I would like to work on this issue and have started preparing a pull request.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions