Issue
I have a POST API in spring boot Restcontroller which accepts a string and returns back the string. But the recieved string value has special characters as "="
@RestController
public class MyApi{
@PostMapping(path = "/", consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
public String parseInput(@RequestBody String data) {
return data;
}
}
curl -d "1" http://localhost:1337/
gives 1=
instead of 1
Solution
Source : https://www.baeldung.com/spring-url-encoded-form-data#1-formhttpmessageconverter-basics
This helped me :
@PostMapping(
path = "/feedback",
consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
public ResponseEntity<String> handleNonBrowserSubmissions(
@RequestParam MultiValueMap<String,String> paramMap) throws Exception {
// Save feedback data
return new ResponseEntity<String>("Thank you for submitting feedback", HttpStatus.OK);
}
I just had to change the data type in the method argument to "@RequestParam MultiValueMap<String,String> paramMap
"
So when I do curl -d '1' http://localhost:1337/
, I had to extract the key separately like this :
String data = (String) ((Map.Entry) paramMap.entrySet().toArray()[0]).getKey();
Answered By - Anthony Vinay
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.