How to decode http POST data in Java?

16,481

Solution 1

Netty has an advanced POST request decoder (HttpPostRequestDecoder) which can decode Http Attributes, FileUpload Content with chunked encoding.

Here is an simple form decoding example

public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
  HttpRequest request = (HttpRequest) e.getMessage();
  HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(new DefaultHttpDataFactory(false), request);

  InterfaceHttpData data = decoder.getBodyHttpData("fromField1");
  if (data.getHttpDataType() == HttpDataType.Attribute) {
     Attribute attribute = (Attribute) data;
     String value = attribute.getValue()
     System.out.println("fromField1 :" + value);
  }
}

Solution 2

You can use HttpPostRequestDecoder in Netty 4.x. It supports all kinds of POST body. Netty 4.x is marked as alpha at the moment, but very stable. See BodyParser in Xitrum.

If you only need to parse simple body, you can still use QueryStringDecoder in Netty 3.x by treating the POST body like the part after "?" in URL, like this:

QueryStringDecoder decoder = new QueryStringDecoder("?" +
    request.getContent.toString(org.jboss.netty.util.CharsetUtil.UTF_8));

Solution 3

Which version of netty are you using? Netty's HttpRequest supports POST method. Not aware of any library which could parse bytes to map of params. This is usually what a servlet container does. Take a look at tomcat's source on how they have implemented processParameters() method http://svn.apache.org/repos/asf/tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/http/Parameters.java

Share:
16,481
ccleve
Author by

ccleve

Updated on August 11, 2022

Comments

  • ccleve
    ccleve over 1 year

    I'm using Netty, and I've got to accept and parse http POST requests. As far as I can tell, Netty doesn't have built-in support for POSTs, only GETs. (It's a fairly low-level library that deals with primitive network operations. Using a servlet container, which does all this stuff out of the box, is not an option.)

    If I have the content of a POST request as an array of bytes, what's the fastest and most bug-free way to parse it into a Map of parameters?

    I could write this myself, but there must be some methods built into the JDK that make this easier. And I'll bet there are some gotchas and corner cases to deal with.

  • ccleve
    ccleve over 12 years
    This is helpful. Looks like they march through the bytes and split on '=' and '&'. I may have to do something similar.
  • ccleve
    ccleve over 12 years
    Are you sure? I googled that class, and found only a third-party library named "Netty Extension" that has it. It that what you're talking about?
  • Jestan Nirojan
    Jestan Nirojan over 12 years
    Sorry I was referring to Netty 4.0.X which is not available yet. As you have said, HttpPostRequestDecoder from the Netty Extension library, which is now merged with Netty 4.0.X. You can download it from here sourceforge.net/projects/goldengate/files/NettyExtension/1.1‌​.9
  • Ngoc Dao
    Ngoc Dao almost 11 years
    Get the JSON string from the request body, then use JSON processing libraries like Jackson (Java) or JSON4S (Scala) etc. to process it: String jsonString = request.getContent.toString(org.jboss.netty.util.CharsetUtil‌​.UTF_8);
  • Mitaksh Gupta
    Mitaksh Gupta over 10 years
    How to send query parameters using Get method in Netty?