'convert text form-data curl request into java code using rest template
curl --location --request POST 'some/api' \
--header 'Content-Type: multipart/form-data' \
--form 'name=testing' \
--form 'Key=testing' \
--form 'Client_Code=123' \
--form 'userid=123' \
Curl request converted using RestTemplate like below. It is not sending the form-data in the body.
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
// headers.setContentType(MediaType.MULTIPART_FORM_DATA); tried this also
MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
formData.add("MsgCode", "testing");
formData.add("Key", "testing");
formData.add("Client_Code", "123");
formData.add("userid", "123");
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(formData, headers);
try {
ResponseEntity<String> responseEntity = restTemplate
.exchange("some/api", HttpMethod.POST,
request, String.class);
Solution 1:[1]
old post, but was looking for something similar and saw nothing wrong other than the key you sent was
formData.add("MsgCode", "testing");
Should have been
formData.add("name", "testing");
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | bobbybobbobbed |
