user2315104
user2315104

Reputation: 2730

How to set the body in POST call using rest Template

I'm trying to execute the URL by making POST http call using rest Template. However, not able to send the message body in the POST request.

Here is the code:

public static void main(String[] args) {
                
        RestTemplate restTemplate = new RestTemplate();
        
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> entity = new HttpEntity<>("Hello World 3", headers);
        headers.setBearerAuth("token");
        headers.add("Header", "header1");
        
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("www.message.com/messages");

        HttpEntity<String> response = restTemplate.exchange(
                builder.toUriString(), 
                HttpMethod.GET,
                entity, 
                String.class);
    }

actually, I'm sending the body using HttpEntity but when Im getting the response, the response is not proper because if the body would have been sent properly, we would have received all the metadata in the response.

Can someone please suggest on whether it is the right way to set the body in POST request.

UPDATE 1

I used below code , still getting error about invalid payload

public static void main(String[] args) {

        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setBearerAuth("token");
        headers.add("Header", "header1");
        String body = "hello world 4";
        HttpEntity<String> entity = new HttpEntity<String>(body, headers);
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("www.example.com");
        HttpEntity<String> response = restTemplate.exchange(
                builder.toUriString(),
                HttpMethod.POST,
                entity,
                String.class);
        System.out.println(response.getBody());

}

getting below error :

Exception in thread "main" org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request: "{"error":{"code":"BadRequest","message":"Unable to read JSON request payload. Please ensure Content-Type header is set and payload is of valid JSON format.","innerError":{"date":"2023-06-21T16:17:39","request-id":"d26d4986-35e0-443c-801f-babf1b741757","client-request-id":"d26d4986-35e0-443c-801f-babf1b741757"}}}" at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:103) at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:183) at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:137) at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63) at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:915) at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:864) at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:764) at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:646) at oe.kubeapi.cloudcontroller.TeamMes4.main(TeamMes4.java:24)

Upvotes: 0

Views: 1230

Answers (2)

Asgar
Asgar

Reputation: 2422

You are using GET instead of POST, Use HttpMethod.POST instead of HttpMethod.GET. And as per your payload, the message clearly says 400 Bad Request, can even see in the message:

"message":"Unable to read JSON request payload. Please ensure Content-Type header is set and payload is of valid JSON format."

this should ring the bell.

You are sending a plain text as your payload which is not accepted by your calling API. Change it into proper JSON format. Something like:

{
    "field1": "value1",
    "field2": "value2"
}

Upvotes: 0

Abhishek Kotalwar
Abhishek Kotalwar

Reputation: 528

had same issue. In my case problem was @RequestBody. It seems that it don't need @RequestBody when your are accepting simple String as body as you did in your example.

String body = "Hello World 3";

   @GetMapping
    public String getData(String value) throws Exception {
        return "test response";
    }

Upvotes: 0

Related Questions