The Geeky Asian

Everything You Need To Know

  • Home
  • Contact
  • Github
  • Discord Server

HTTP GET Request with a Request Body using RestTemplate

August 18, 2021 by thegeekyasian 5 Comments

ShareTweet

Lets have a look on how to send HTTP GET Request with a Request Body using RestTemplate!

Solved HTTP GET Request with a Request Body using RestTemplate

As much as I dislike the idea of having a HTTP GET request with a request body, I recently got stuck with one. I had to call an external service endpoint, in one of my projects last week. Sadly it was a HTTP GET API endpoint but it required a… wait for it… REQUEST BODY!

The project uses RestTemplate as a main HTTP Client for all API calls. I tried using exchange method with different combinations or request body and headers but failed, as the request body wasn’t being sent to the external service. Guess what’s the worse part? They didn’t have meaningful error messages either. IKR?!

After spending more than 4 hours, I figured out that RestTemplate doesn’t support Http Get request with a Request Body by default. As the RestTemplate was massively used throughout the project, I couldn’t migrate to any other client. I figured out a way to keep using RestTemplate while making sure the request body is sent in the GET request – and that too without any hacks.

Here is a small code that you need to add to your spring project and it would start working.

import java.net.URI;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpUriRequest;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
.
.
.

public class Client {

private final RestTemplate restTemplate;

public Client() {
	restTemplate = new RestTemplate(new CustomHttpComponentsClientHttpRequestFactory());
}

private void getWithBody(Object requestBody) {

	String responseBody = restTemplate.exchange(endpoint, HttpMethod.GET, new HttpEntity<>(requestBody), String.class).getBody();

}

private static final class CustomHttpComponentsClientHttpRequestFactory extends HttpComponentsClientHttpRequestFactory {

	@Override
	protected HttpUriRequest createHttpUriRequest(HttpMethod httpMethod, URI uri) {

		if (HttpMethod.GET.equals(httpMethod)) {
			return new HttpEntityEnclosingGetRequestBase(uri);
		}
		return super.createHttpUriRequest(httpMethod, uri);
	}
}

private static final class HttpEntityEnclosingGetRequestBase extends HttpEntityEnclosingRequestBase {

	public HttpEntityEnclosingGetRequestBase(final URI uri) {
		super.setURI(uri);
	}

	@Override
	public String getMethod() {
		return HttpMethod.GET.name();
	}
}
}

Now that we have the config is ready to use, lets override the request-factory for our RestTemplate instance. All you need need to do is use the setRequestFactory method of your RestTemplate instance and inject a new instance of CustomHttpComponentsClientHttpRequestFactory class

restTemplate.setRequestFactory(new CustomHttpComponentsClientHttpRequestFactory());

And… you are welcome mates!

Do not forget to share this with your team mates, if that solves your problem. Cheers 🙂

ShareTweet

Filed Under: How to?

Your thoughts? Feedback? Anything?

Comments

  1. Paul J. Suraez says

    August 20, 2021 at 6:01 pm

    BRILLIANT! I’ve been scratching my head why my request is not going through!

    Thanks youu!!!

    Reply
  2. Fabrice says

    December 3, 2021 at 12:52 pm

    Thank you very much!!

    Reply
  3. Brandon Gallagher says

    January 8, 2022 at 2:17 am

    thanks! that was super helpful and easy to implement.

    Reply
  4. Mike Y says

    April 11, 2022 at 2:02 pm

    You are a genius my man

    Reply
  5. Sadik says

    March 7, 2023 at 10:35 am

    dude you deserve respect

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

How to?

  • WebSockets
  • JasperReports with Custom Fonts
  • Get Request with Body using RestTemplate

Like me?!

Copyright © 2023 · The Geeky Asian