-3

I am sending some data from my react app through Axios to my backend on Eclipse through servlets. My POST request has the right payload and even when I inspect the request with the code request.getReader().lines().collect(Collectors.joining(System.lineSeparator())), I get the entire contents of the request. It's a JSON format:

{"business_code":"90","number":"90","date":"2022-01-10"...

But when I try to get the value from any of the fields, it stores a null string and my numerical fields then throw an exception. Null string:

System.out.println("Bizz Code" + request.getParameter("business_code"));

Bizz Codenull

I don't understand why I am getting null when everything is actually there. Why is this happening?

Rick45888
  • 155
  • 1
  • 2
  • 9

1 Answers1

0

Take a good look at the doc for getReader() and getParameter(). They do not access the same parts of an HTTP request--getReader() operates on the body, not the parameters.

While you have extracted the body of the request into a String, you'll need to parse the json from the string to do what you're trying to do. Often you'll see Jackson or GSON used for this, but typically they're used with a framework for building services, and not raw Servlets.

nitind
  • 19,089
  • 4
  • 34
  • 43
  • I think Gson can be used in Java with Eclipse, did that before. Having a few issues with the Gson data type parsing but will follow-through. Thanks for the answer! – Rick45888 Apr 05 '22 at 11:46