How to decode http post data in Java?

I am using Netty and I need to accept and parse http POST requests. As far as I can tell, Netty doesn't have native POST support, only GET. (This is a rather low-level library that deals with primitive network operations. Using a servlet container that does all this out of the box is not an option.)

If I have the content of the POST request as an array of bytes, what is the fastest and easiest way to parse it in Map of parameters?

I could write this myself, but there should be built-in methods in the JDK that simplify this. And I bet that there are some problems associated with the launch and the angle of view.

+6
source share
3 answers

Netty has an advanced POST request decoder (HttpPostRequestDecoder) that can decode Http Attributes, FileUpload Encrypted Content.

Here is an example of a simple form decoding

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); } } 
+13
source

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

If you just need to parse a simple body, you can use QueryStringDecoder in Netty 3.x, treating the POST body as part after the "?" in the URL, for example:

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

What version of netty are you using? Netty HttpRequest supports the POST method. No library is known that could parse bytes to display parameters. This is usually a servlet container. Take a look at the tomcat source on how they implemented the processParameters () method http://svn.apache.org/repos/asf/tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/http/ Parameters.java

+2
source

Source: https://habr.com/ru/post/904615/


All Articles