0

I am using Spring-Boot-Web.

This is my handler for a web-request.

@Component
@RequestMapping ("/foo")
@CrossOrigin (maxAge = 3600)
@RestController
public class WebHandler
{   
    @RequestMapping ("/bar")
    public String bar () throws Exception
    {   
        return "blabla";
    }
}

I need to get the full raw request string (HTTP-header and body) into the handler.

How can I do that?

Georgii Lvov
  • 1,771
  • 1
  • 3
  • 17
chris01
  • 10,921
  • 9
  • 54
  • 93
  • 1
    Does this answer your question? [How to get access to HTTP header information in Spring MVC REST controller?](https://stackoverflow.com/questions/19556039/how-to-get-access-to-http-header-information-in-spring-mvc-rest-controller) – fukit0 Jul 07 '21 at 14:53
  • Its not the raw data but its a start. – chris01 Jul 07 '21 at 14:58
  • What is raw data for you ? – fukit0 Jul 07 '21 at 14:59
  • All bytes that are sent to the server - in the view of the application-layer (the socket). – chris01 Jul 07 '21 at 15:03

1 Answers1

2

Perhaps option with HttpServletRequest will suit you?

@RequestMapping("/foo")
@CrossOrigin(maxAge = 3600)
@RestController
public class WebHandler {

    @RequestMapping("/bar")
    public void bar(HttpServletRequest request) throws Exception {

        BufferedReader reader = request.getReader();
        String body = reader.lines().collect(Collectors.joining("\n"));
        System.out.println(body);

        Enumeration<String> headerNames = request.getHeaderNames();

        while (headerNames.hasMoreElements()) {
            String headerName = headerNames.nextElement();
            System.out.println(headerName + ": " + request.getHeader(headerName));
        }
    }
}

You can also see for yourself what other information you can get from there.

If you need only body and headers, you can make it so:

    @RequestMapping("/bar")
    public void bar(@RequestHeader Map<String, String> headers,
                    @RequestBody String body) {

        System.out.println(body);
        System.out.println(headers);
    }
Georgii Lvov
  • 1,771
  • 1
  • 3
  • 17