How to get HTTP request and response headers on a Spring Boot Application
search cancel

How to get HTTP request and response headers on a Spring Boot Application

book

Article ID: 298281

calendar_today

Updated On:

Products

VMware Tanzu Application Service for VMs

Issue/Introduction

In Tanzu Application Service for VMs (TAS for VMs), the Gorouter or application logs does not include HTTP request and response headers. 

This article discuss on how you can get HTTP request and response headers on a Spring Boot Application without making any extra change in your code and only using Spring Boot's features.

Environment

Product Version: 2.11

Resolution

Spring Boot includes production ready functionality with the Sping boot actuator. On of the endpoint, there is httptrace, which displays HTTP trace information and includes request headers and response headers. By default it will display the last 100 Http request-response exchanges.

To get HTTP request and response headers on your Spring Boot Application, follow these steps:

1. Enable Spring Boot Actuator. For detail information on how to enable to Spring Boot Actuator, refer to Spring Boot Actuator: Production-ready Features.

2. Expose endpoints on your application.yml.

For example, adding this to your YML file exposes all endpoints:

management:
  endpoints:
    web:
      exposure:
        include: "*"


3. Response headers are not included by default, you need to add on them to application.yml.

management:
  endpoints:
    web:
      exposure:
        include: "*"
  trace
    http
      include: "RESPONSE_HEADERS"


4. Access the HTTP trace endpoint through http://application-route/actuator/httptrace. This gives you a list of 100 request-response exchanges.

For example, here is a sample one request-response exchange that includes headers:

{
         "timestamp":"2021-10-18T05:39:42Z",
         "principal":null,
         "session":null,
         "request":{
            "method":"GET",
            "uri":"http://spring-music-comedic-bat-pe.cfapps-05.slot-59.pez.vmware.com/templates/grid.html",
            "headers":{
               "referer":[
                  "http://spring-music-comedic-bat-pe.cfapps-05.slot-59.pez.vmware.com/"
               ],
               "x-forwarded-proto":[
                  "http"
               ],
               "accept-language":[
                  "en-GB,en-US;q=0.9,en;q=0.8"
               ],
               "x-cf-instanceid":[
                  "26686263-4348-4f83-4537-a99e"
               ],
               "x-vcap-request-id":[
                  "b0c5727b-661e-41df-58cf-1a293dbc2501"
               ],
               "accept":[
                  "application/json, text/plain, */*"
               ],
               "x-cf-applicationid":[
                  "1cf18a4c-913e-4714-bc7b-3718f09e2806"
               ],
               "b3":[
                  "b29826d3022af7b8-b29826d3022af7b8"
               ],
               "x-b3-traceid":[
                  "b29826d3022af7b8"
               ],
               "x-b3-spanid":[
                  "b29826d3022af7b8"
               ],
               "x-request-start":[
                  "1634535581998"
               ],
               "host":[
                  "spring-music-comedic-bat-pe.cfapps-05.slot-59.pez.vmware.com"
               ],
               "x-cf-instanceindex":[
                  "0"
               ],
               "accept-encoding":[
                  "gzip, deflate"
               ],
               "user-agent":[
                  "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36"
               ]
            },
            "remoteAddress":null
         },
         "response":{
            "status":200,
            "headers":{
               "Accept-Ranges":[
                  "bytes"
               ],
               "Last-Modified":[
                  "Tue, 12 Nov 2019 13:41:52 GMT"
               ],
               "Content-Length":[
                  "1419"
               ],
               "Date":[
                  "Mon, 18 Oct 2021 05:39:41 GMT"
               ],
               "Content-Type":[
                  "text/html"
               ]
            }
         },
         "timeTaken":6
      },