How to decode http POST data in Java?

后端 未结 3 1333
臣服心动
臣服心动 2021-02-06 11:14

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-leve

3条回答
  •  萌比男神i
    2021-02-06 11:45

    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);
      }
    }
    

提交回复
热议问题