简体   繁体   中英

Sending same request headers into the response headers cause infinity loop and incorrect json response Spring boot

I want to return the same request headers in my controller response but if I do this and infinity wait look occurs when trying to test with postman

I have this controller

@PostMapping("/yy")
public ResponseEntity<ClientOutput> myTest(@RequestHeader HttpHeaders headers,
                                             @RequestBody ClientInput clientInput) {
    return new ResponseEntity<>(new ClientOutput(), headers, HttpStatus.OK);

  }

this cause an infinity waiting loop when I try to test it with postman, how can I return the same headers that I get from my request in my response,

And it also produce an incomplete response when I try with this controller

@PostMapping("/uu")
  public ResponseEntity<ClientOutput> myTestTwo(@RequestHeader HttpHeaders headers,
                                             @RequestBody ClientInput clientInput) {
    return new ResponseEntity<>(ClientOutput.builder()
            .error(Error.builder()
                    .code("401")
                    .title("Error")
                    .message("A error happened")
                    .build())
            .build(), headers, HttpStatus.UNAUTHORIZED);

  }

instead of returning my error DTO it returns this incomplete JSON

{
    "name": null,
    "error": {
        "code": "401",
        "title": "Error",

I just want to return the same request headers in my response

Postman is sending by default the Header Content-Length and it is calculated for the request. Since your code is just taking the Content-Length header of the request and returns it, it will not match the real length of the response.
Removing the request header Content-Length in Postman will fix the issue that your response is an incomplete JSON structure.

In Postman open Headers tab for request and make sure auto-generated headers are not hidden. Then you can uncheck Content-Length header.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM