How to Do Post Request With Raw Data Via Spring Rest Template

How to send body content as raw JSON and not form-data in Spring Boot RestTemplate

Thanks to @Alex Salauyou based on his comment using HashMap instead of MultiValueMap solved the problem. Here is the changes need to be done:

HashMap<String, String> bodyParam = new HashMap<>();
bodyParam.put("amount", request.getAmount());
bodyParam.put("destinationNumber",request.getDestinationNumber());

How to send POST request through RestTemplate with custom parameter in header

From the discussion in the comments, it's quite clear that your request object isn't correct. If you are passing a plain string containing folder name, then you don't need a MultiValueMap. Just try sending a string,

    String data = "/public/"
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.TEXT_PLAIN);

HttpEntity<String> request = new HttpEntity<String>(
data, headers);
String url = "http://192.168.1.51:8080/pi/FilesServlet";
restTemplate.getMessageConverters().add(new FormHttpMessageConverter());
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
String response = restTemplate
.postForObject(url, request, String.class);

sending GET request via REST template with JSON request body getting failed with binding binding element must be a struct error?

rest template doesn't support get request with body . for more details you can refer this article.

If you are on Java 11 I would suggest you to use java.net.HttpClient which will fulfill your need.



Related Topics



Leave a reply



Submit