1

I'm trying to create a little test servlet (Tomcat 10) to accept simple "POST" messages, however no matter what I've tried the servlet never seems to get the body (json).

I'm using Postman to send the messages (http).

I've tried getReader() and getInputStream() and both keep returning nothing. I've read that this can happen when the buffer has already been read, but I'm not reading it twice (not that I am aware of).

Here are some debug values:

getContentLength() => -1

getContentType() => application/json

getReader().ready() => false.

So, either no body was recieved or it was already read - but where? I've been using Postman for other API's without issue, so I don't believe I've done something wrong there, what else can I try within the servlet?

Does anyone have any suggestions of what I could try to get this working?

Update

Thank you @parsifal, I tested the servlet using PowerShell's Invoke-WebRequest and it worked perfectly, so I took another look at all my Postman settings, tested several things and then finally found my error. The Header parameter Content-Length wasn't selected - I had incorrectly assumed that it would be automatically sent, but it wasn't.

Eric
  • 1,321
  • 2
  • 15
  • 30
  • A POST request body consists of name-value pairs, and they are available via `getRequestParameter()`, not as the body. They are also decoded for you. – user207421 May 09 '22 at 06:26
  • Hi @user207421, are you really sure about that? In postman I sent the "json" as "raw" (application/json) there is no option for providing a name/value pair for the body. Headers have name/value pairs. At any rate, I'll check/debug the parameters to see if I can find the payload/body. – Eric May 09 '22 at 07:08
  • Well you shouldn't have sent it that way, because you can't retrieve it that way, You should have sent it as a named parameter of the POST request. – user207421 May 09 '22 at 07:27
  • 1
    You should have no problem retrieving the data this way, but the content length of -1 indicates to me that you're not actually sending a body. Perhaps try with `curl` rather than Postman? (@EJP - the only time `HttpServletRequest` should try to process the body as form data is if (1) you specify content type as `application/x-www-form-urlencoded`, and (2) the servlet code requests parameter values). – Parsifal May 10 '22 at 01:08

0 Answers0