Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I retrieve POST data from a Jetty AbstractHandler handle() method?

Searched SO quite a bit, but didn't see any posts that seemed to match my particular situation/question.

Using Jetty, I have a Handler class extending org.eclipse.jetty.server.handler.AbstractHandler.

The handle method is as follows:

@Override
    public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
                       throws IOException, ServletException {

I need to grab the POST data sent in the request, as if like this:

curl -H "Content-Type: application/json" -d '{"url":"http://www.example.com"}' http://localhost:8080/

Basically, I'm sending a POST request to my localhost with a JSON dictionary keyed on 'url'. How can I retrieve that POST data?

like image 599
srirachablender Avatar asked Jan 09 '23 20:01

srirachablender


1 Answers

Use the standard servlet features available to from the HttpServletRequest parameter in the Handler.handle() method.

The HttpServletRequest has 2 methods that have access to the request body.

  • For text content, use HttpServletRequest.getReader()
  • For binary content, use HttpServletRequest.getInputStream()

Then just use either of those 2 using standard java IO techniques.

like image 146
Joakim Erdfelt Avatar answered Jan 25 '23 01:01

Joakim Erdfelt