I want to convert the following (working) curl snippet to a RestTemplate call:
curl -i -X POST -d "email=first.last@example.com" https://app.example.com/hr/email
How do I pass the email parameter correctly? The following code results in a 404 Not Found response:
String url = "https://app.example.com/hr/email";
Map<String, String> params = new HashMap<String, String>();
params.put("email", "first.last@example.com");
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.postForEntity( url, params, String.class );
I've tried to formulate the correct call in PostMan, and I can get it working correctly by specifying the email parameter as a "form-data" parameter in the body. What is the correct way to achieve this functionality in a RestTemplate?
here is the full program to make a POST rest call using spring's RestTemplate.
Your url String needs variable markers for the map you pass to work, like:
Or you could explicitly code the query params into the String to begin with and not have to pass the map at all, like:
See also https://stackoverflow.com/a/47045624/1357094
The POST method should be sent along the HTTP request object. And the request may contain either of HTTP header or HTTP body or both.
Hence let's create an HTTP entity and send the headers and parameter in body.
http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html#postForObject-java.lang.String-java.lang.Object-java.lang.Class-java.lang.Object...-
How to POST mixed data: File, String[], String in one request.
You can use only what you need.
The POST request will have File in its Body and next structure: