Similar to my previous article, we had the need to do some changes with incoming HTTP-Headers in Quarkus: users were calling the /openapi.json endpoint without requesting a specific media type, which made Quarkus return Yaml.

Bild von succo auf Pixabay

Users are so accustomed to assume that the suffix of a URL-path also determines the media type — probably because it is like this on Windows — that they expected calling /openapi.json to return data in Json format.

Looking at the Quarkus source code shows that the default media type for the OpenAPI data is Yaml. If the user wants Json, an additional HTTP Accept header would need to be passed, which the user code did not supply (because it already has the suffix).

Vert.x router to the rescue

As I have shown before, all HTTP-Requests pass the Vert.x Web Router layer of Quarkus:

Path of HTTP Requests in Quarkus

Which means that we can use a Vert.x RouteFilter to do the work:

@RouteFilter(401) // (1)

void oasAcceptHeaderMangler(RoutingContext rc) {

if (rc.normalisedPath().endsWith("openapi.json")) { // (2)

rc.request().headers().remove("Accept"); // (3)

rc.request().headers()

.add("Accept","application/json");

}

rc.next(); // (4)

}

We annotate the method with RouteFilter in (1). If our path ends with “openapi.json”, we start modifying the request(2). As we look at the normalisedPath() from the RoutingContext we don’t need to worry about query parameters unlike the case in my other post, where we had to use the HttpRequest-URI.

As next step I remove the old Accpet-Header when it exists and add one that requests Json (3). Last, but not least we tell Vert.x to call the next filter in the chain or dispatch to the next layer(4). Quarkus will seem to hang if we forget this.

Conclusion

The Vert.x web router is the logical point within Quarkus to modify HTTP requests independent of the used application framework in higher layers. In the previous article we have seen how it can be used to change the request path.

The Reactive Routing guide tells more about Quarkus and its Vert.x web router.