Home > OS >  Rest API : Post API Throwing 400 Bad request
Rest API : Post API Throwing 400 Bad request

Time:12-11

Use Case -

I am having a post API which is supposed to consume Content-Type = x-www-form-urlencoded. When creating individual boot project and implementing POST API, I am getting Form Data in my controller(testing it using postman), but when the same code I integrate it in my project which is Spring MVC project, whenever I test it always gives me 400 Bad request in Response.

  @RequestMapping(value = "/registration", method = RequestMethod.POST,consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public void marketplaceRegistration(@RequestBody MultiValueMap<String, String> formData, HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException {
    LOGGER.info("\nIn registration method: TOKEN RECIEVED IS " formData.getFirst("token"));}

this is my method where I will be receiving request. I have used @RequestBody , @RequestParam Both annotation and both works fine with the spring boot applciation but when I am integrating this is throwing 400 Bad request.

The only difference is we are having a WsFilter from apache in place. Not sure what is wrong but if anyone can help that would be great.

CodePudding user response:

So Apparently, In My Project where I was integrating the Post API with ContentType is x-www-form-urlencoded, a filter was reading the request and due to which the request body of urlencoded form data was getting lost. Upon removing the filter, my request are going through controller and I am able to get the 200 response.

Unfortunately can't post the filter logic here.

  • Related