TryHard
TryHard

Reputation: 379

Get request header in spring boot

How do I get the header and body of the current request from an application which called my Springboot application? I need to extract this information. Unfortunately this does not work. I tried to get the current request with this code sample (https://stackoverflow.com/a/26323545/5762515):

public static HttpServletRequest getCurrentHttpRequest(){
    RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
    if (requestAttributes instanceof ServletRequestAttributes) {
        HttpServletRequest request = ((ServletRequestAttributes)requestAttributes).getRequest();
        return request;
    }
    throw new IllegalArgumentException("Request must not be null!");
}

And then I tried to get the body

ContentCachingRequestWrapper requestWrapper = (ContentCachingRequestWrapper) currentRequest;
    String requestBody = new String(requestWrapper.getContentAsByteArray());

Can someone tell me what im doing wrong? Thanks in advance

Upvotes: 3

Views: 28524

Answers (3)

Kemot 90
Kemot 90

Reputation: 331

you can get header with your code but need apply some changes.

private String getRequest() throws Exception {
        RequestAttributes attribs = RequestContextHolder.getRequestAttributes();
        if (attribs != null) {
            HttpServletRequest request = ((ServletRequestAttributes) attribs).getRequest();
            return request ;
        }
        
        throw new IllegalArgumentException("Request must not be null!");
}

after you can extract header info from request. For example if you want get Accept-Encoding

String headerEncoding = getRequest().getHeader("Accept-Encoding");

obliviusly you don't use this approce if not necessary.

If you want exract the body NOT use this solution

Upvotes: 2

Dhwanil Patel
Dhwanil Patel

Reputation: 2573

@TryHard, You're using spring boot then following way is more preferable for you,

@RestController
public class SampleController {
    @RequestMapping("/get-header-data")
    public ResponseEntity<?> sampleEndpoint(HttpServletRequest request) {
        // request object comes with various in-built methods use as per your requirement.
        request.getHeader("<key>");
    }
}

Upvotes: 1

Briano Bruno
Briano Bruno

Reputation: 66

@RestController
public class SampleController {

    @PostMapping("/RestEndpoint")
    public ResponseEntity<?> sampleEndpoint(@RequestHeader Map<String, String> headers,@RequestBody Map<String,String> body) {
        //Do something with header / body
        return null;
    }
}

If the application's are communicating through a rest endpoint I believe this would be the simplest solution. In spring you can add RequestHeader and RequestBody annotations to method arguments to have them setup to be used.

Of course you can map RequestBody directly to some POJO instead of using a map but just as an example.

Let me know if this is what you were looking for !

Upvotes: 3

Related Questions