ContentType header with json schema URL parameter in Spring WebClient

Viewed 378

I'm using Spring to integrate towards a third party API. This API validates the Content-Type header and requires it to be exactly application/json;profile=https://se.digitalreceipts.net/schemas/json/retail/v1.0

The code below is the relevant part of the POST call i make towards the API.

    webClient.post()
        .uri("/dr/v1/receipts")
        .attributes(clientRegistrationId(clientRegistrationId))
        .header("Content-Type", "application/json;profile=https://se.digitalreceipts.net/schemas/json/retail/v1.0")

When executing this request an exception is thrown:

org.springframework.http.InvalidMediaTypeException: Invalid mime type "application/json;profile=https://se.digitalreceipts.net/schemas/json/retail/v1.0": Invalid token character ':' in token "https://se.digitalreceipts.net/schemas/json/retail/v1.0"
    at org.springframework.http.MediaType.parseMediaType(MediaType.java:620) ~[spring-web-5.3.10.jar:5.3.10]

Seems that spring does not allow colons in the Content-Type header. This is an issue as the API I'm integrating towards absolutely requires the content-type header to be exactly as previously specificated. I can't for instance write the value with quotation marks application/json;profile="https://se.digitalreceipts.net/schemas/json/retail/v1.0"

I'm looking for a solution where I can enter the required Content-Type header using Spring WebClient.

1 Answers

The Content-Type header must follow the format described on RFC 2045 section 5.1. It is stated that special characters must be in quoted-string, to use within parameter values.

Spring parses the Content-Type header before executing the REST API call and validates the mimeType. If we quote the value of profile attribute then the mimeType is valid. For example:

webClient.post()
    .uri("/dr/v1/receipts")
    .attributes(clientRegistrationId(clientRegistrationId))
    .header("Content-Type", "application/json;profile=\"https://se.digitalreceipts.net/schemas/json/retail/v1.0\"")
Related