Tuesday, 21 May 2024

Configure default http request headers at client level

We can assign default request header values to HttpClient using setDefaultHeaders method.

List<Header> defaultHeaders = new ArrayList<>();
defaultHeaders.add(new BasicHeader("default-header-1", "default-value-1"));

CloseableHttpClient httpClient = HttpClients
.custom()
.setDefaultHeaders(defaultHeaders)
.build();

Find the below working application.

SetDefaultHeadersToHttpClient.java
package com.sample.app.httpclient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.ClassicHttpRequest;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.HttpClientResponseHandler;
import org.apache.hc.core5.http.message.BasicHeader;

public class SetDefaultHeadersToHttpClient {

	public static String inputStreamToString(InputStream inputStream, Charset charSet) throws IOException {

		try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, charSet))) {
			return reader.lines().collect(Collectors.joining("\n"));
		}

	}

	public static void main(String[] args) throws Exception {

		HttpClientResponseHandler<String> responseHandler = new HttpClientResponseHandler<String>() {
			@Override
			public String handleResponse(final ClassicHttpResponse response) throws IOException, ParseException {

				HttpEntity httpEntity = response.getEntity();

				try (InputStream is = httpEntity.getContent()) {
					return inputStreamToString(is, StandardCharsets.UTF_8);
				}

			}
		};

		List<Header> defaultHeaders = new ArrayList<>();
		defaultHeaders.add(new BasicHeader("default-header-1", "default-value-1"));

		try (CloseableHttpClient httpClient = HttpClients.custom().setDefaultHeaders(defaultHeaders).build();) {

			ClassicHttpRequest request = new HttpGet("http://localhost:8080/api/v1/replay-request");
			request.setHeader("test-header-1", "test-value-1");
			request.setHeader("test-header-2", "test-value-2");

			String response = httpClient.execute(request, responseHandler);
			System.out.println(response);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

 Output

{"headers":{"host":"localhost:8080","test-header-1":"test-value-1","connection":"keep-alive","test-header-2":"test-value-2","default-header-1":"default-value-1","accept-encoding":"gzip, x-gzip, deflate","user-agent":"Apache-HttpClient/5.3.1 (Java/1.8.0_333)"},"parameters":{},"attributes":{},"payload":""}

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment