Search code examples
javaspringrestspring-boothttp-trace

How can I log the Content-Type that Spring rejects with a 415 Unknown Media Type?


I've got a basic Spring Boot app with a REST endpoint that is configured to receive POSTs with Content-Type = application/json.

My external partner is posting me HTTP requests, but apparently with a different Content-Type, because my app rejects those requests with HTTP 415 Unsupported Media Type.

Unfortunately, Spring's logs do not reveal what exactly the Content-Type is, and the external partner is currently unavailable for questions.

Is there a way to crank up Spring's log level so the log also includes the received Content-Type that's being rejected?


Solution

  • I see the following options as stated in the comments above:

    1. Use an external packet analyser like wireshark
    2. Use the httptrace actuator you can see an example here
    3. Write an interceptor as you can see here

    Use the httpTrace actuator

    The httptrace provides information about the HTTP request/response exchange. It can be called by doing a get to /actuator/httptrace. One can use curl:

    $ curl 'http://localhost:8080/actuator/httptrace' -i -X GET
    

    or directly from your browser, on your local machine that would be http://localhost:8080/actuator/httptrace.

    The information is provided as JSON:

    HTTP/1.1 200 OK
    Content-Type: application/vnd.spring-boot.actuator.v3+json
    Content-Length: 503
    
    {
      "traces" : [ {
        "timestamp" : "2019-12-06T06:13:02.341Z",
        "principal" : {
          "name" : "alice"
        },
        "session" : {
          "id" : "41a5c57b-112a-4b15-8ea9-05c5942e7e88"
        },
        "request" : {
          "method" : "GET",
          "uri" : "https://api.example.com",
          "headers" : {
            "Accept" : [ "application/json" ]
          }
        },
        "response" : {
          "status" : 200,
          "headers" : {
            "Content-Type" : [ "application/json" ]
          }
        },
        "timeTaken" : 1
      } ]
    }