Skip to content
Advertisement

400 Bad Request when using Spring Boot

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!

Advertisement

Answer

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 "{}";
    }
}
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement