How can I get POST data from Jetty AbstractHandler descriptor method?
Searched SO quite a bit but didn't see any posts that seemed to fit my particular situation / question.
Using Jetty, I have a Handler class that extends org.eclipse.jetty.server.handler.AbstractHandler.
The descriptor method looks like this:
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
I need to grab the POST data sent in a request like this:
curl -H "Content-Type: application/json" -d '{"url":"http://www.example.com"}' http://localhost:8080/
Basically, I am sending a POST request to my localhost using a JSON dictionary with the key "url". How can I get the POST data?
source to share
Use the standard servlet functions available with a parameter HttpServletRequest
in Handler.handle()
.
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 any of these 2 using standard Java IO methods.
source to share