简体   繁体   中英

How to forward HttpServletRequest headers to another REST API in Spring Boot

I have an REST API which accepts request headers in request. My controller is internally calling another API. What i want to do is pass all headers that i am getting in request to internal API that controller is calling.

I know that i can iterate over header and set them in HttpRequest that I am creating but is there any other way to set in a single step.

Thanks,

You can set headers as below:-

HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(new MediaType[] { MediaType.APPLICATION_JSON }));
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("custom-header-name, "value");

HttpEntity<Request> entity = new HttpEntity<Request>(request, headers);

RestTemplate template = new RestTemplate();

ResponseEntity<Response> respEntity = template
    .exchange("URL", HttpMethod.POST, entity , RestResponse.class);

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