I am sending a Http POST request to my RESTful API build with Spring Boot and get the “400 Bad Request” response.
My POST request is made with Postman, send to
JavaScript
x
http://localhost:8080/executebash
with the body
JavaScript
{
"filename": "blaba"
}
I want to pass the filename
variable to my Java Method.
My RESTful api is build in Java with Spring Boot
JavaScript
@RestController
public class PrapiController {
private Process process;
@RequestMapping(value = "/executebash", produces ="application/json", method = RequestMethod.POST)
public String executeBashScript(@RequestParam String filename) {
//...
}
}
I tried with and with out produces in the @RequestMapping
annotation.
I have no idea where the error comes from, maybe you can help me.
Regards!
Advertisement
Answer
Use @RequestBody
to accept data in request body. As shown in below example:
JavaScript
@RestController
public class PrapiController {
private Process process;
@RequestMapping(value = "/executebash", consumes="application/json", produces ="application/json", method = RequestMethod.POST)
public String executeBashScript(@RequestBody Map<String, String> input) {
String filename = input.get("filename");
return "{}";
}
}