web123456

RestTemplate Use: Set request header and request body

Article Directory

    • HttpEntity ⭐
    • 1. Set request header and request body for post and put requests
    • 2. Set request headers and request bodies for other requests

HttpEntity ⭐

When using RestTemplate, you can set the request header and the request body through HttpEntity. There are 4 constructors for HttpEntity:

  1. Neither the request body nor the request header is set
  2. Set only the request body
  3. Set only the request header
  4. Set request body and request header at the same time

HttpEntity source code:

/**
 * Create a new, empty {@code HttpEntity}.
 */
protected HttpEntity() {
  this(null, null);
}

/**
 * Create a new {@code HttpEntity} with the given body and no headers.
 * @param body the entity body
 */
public HttpEntity(T body) { // Set only the request body
  this(body, null);
}

/**
 * Create a new {@code HttpEntity} with the given headers and no body.
 * @param headers the entity headers
 */
public HttpEntity(MultiValueMap<String, String> headers) { // Set only request headers
  this(null, headers);
}

/**
 * Create a new {@code HttpEntity} with the given body and headers.
 * @param body the entity body
 * @param headers the entity headers
 */
public HttpEntity(T body, MultiValueMap<String, String> headers) { // Set request body and request header at the same time
  this.body = body;
  HttpHeaders tempHeaders = new HttpHeaders();
  if (headers != null) {
      tempHeaders.putAll(headers);
  }
  this.headers = HttpHeaders.readOnlyHttpHeaders(tempHeaders);
}

1. Set request header and request body for post and put requests

If you set the request header and request body for post and put requests, you can use the second parameter to pass into the HttpEntity object when calling the method, for example:

HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("MyRequestHeader", "MyValue");
HttpEntity requestEntity = new HttpEntity(requestHeaders);

Book book = restTemplate.postForObject("http://127.0.0.1:8080/getbook", requestEntity, Book.class);

PS:public class HttpHeaders implements MultiValueMap<String, String>, Serializable

Set the request header and request body at the same time:

    @PostMapping("post_with_body_and_header")
    public void postWithBodyAndHeader(@RequestBody(required = false) UserEntity requestBody) {
        // 1. Request header
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("headerName1", "headerValue1");
        httpHeaders.add("headerName2", "headerValue2");
        httpHeaders.add("headerName3", "headerValue3");
        httpHeaders.add("Content-Type", "application/json"); // Must be set when passing the request body

        // 2. Request header & request body
        HttpEntity<String> fromEntity = new HttpEntity<>(JSONUtil.toJsonStr(requestBody), httpHeaders);


        MessageBox responseBody = restTemplate.postForObject(INVOKE_URL + "/test/receive", fromEntity, MessageBox.class);
        log.info("Responsive body: {}", JSONUtil.toJsonPrettyStr(responseBody));
    }

2. Set request headers and request bodies for other requests

If other HTTP method calls want to set the request header, you can use the exchange() method:

HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("MyRequestHeader", "MyValue");
HttpEntity requestEntity = new HttpEntity(requestHeaders);

HttpEntity<String> response = template.exchange(
        "/hotels/{hotel}",
        HttpMethod.GET, 
        requestEntity, 
        String.class, 
        "42"
    );

String responseHeader = response.getHeaders().getFirst("MyResponseHeader");
String body = response.getBody();