Search code examples
javaspring-bootrestclient

How to attach a body to a GET request when using Spring's new RestClient?


I am writing a client using java and spring framework which will send a get request with body (I am aware that it is not recommended but it is not my decision) to the server using RestClient

I am aware that a similar question already exists (Spring RestTemplate - http GET with request body) but it is implemented using RestTemplate and in my case I am using the new RestClient.

I have tried searching and experimenting but to no avail as there isn't much information regarding RestClient yet. I have an error when I try to add the .body() to the .get(). It works fine for post requests


Solution

  • you can use .method() with the desired http method type and then add the body here is the code:

    restClient.method(HttpMethod.GET).body("your body")