Reputation: 113
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
http://localhost:8080/executebash
with the body
{
"filename": "blaba"
}
I want to pass the filename
variable to my Java Method.
My RESTful api is build in Java with Spring Boot
@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!
Upvotes: 0
Views: 9903
Reputation: 1821
Use @RequestBody
to accept data in request body. As shown in below example:
@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 "{}";
}
}
Upvotes: 3
Reputation: 1085
With @RequestParam
annotation you have to use param in request URL instead of body with JSON:
http://localhost:8080/executebash?filename=blaba
If you want use your JSON, you have to use @RequestBody
with data transfer object or Map<String, String>
like @pcsutar said.
Upvotes: 3