0

I am trying to get the sample Netty HttpUploadServer to receive an uploaded file via HTTP PUT, code found here: https://github.com/netty/netty/blob/master/example/src/main/java/io/netty/example/http/upload/HttpUploadServerHandler.java

To test an HTTP POST file upload, I use this curl command:

curl -F "[email protected]" http://127.0.0.1:8080

To test an HTTP PUT file upload, I use this curl command:

curl -T "testfile.txt" http://127.0.0.1:8080

I commented out the writeMenu and return as i'm using curl and not a web browser. Using curl to POST, everything seems to work fine, however with PUT I am not getting any data in readHttpDataAllRecieve (HttpUploadServerhandler):

private void readHttpDataAllReceive(Channel channel) {
    List<InterfaceHttpData> datas = null;
    try {
        datas = decoder.getBodyHttpDatas();
        System.out.println("size " + datas.size());
    } catch (NotEnoughDataDecoderException e1) {
        // Should not be!
        e1.printStackTrace();
        responseContent.append(e1.getMessage());
        writeResponse(channel);
        Channels.close(channel);
        return;
    }
    for (InterfaceHttpData data: datas) {
        writeHttpData(data);
    }
    responseContent.append("\r\n\r\nEND OF CONTENT AT FINAL END\r\n");
}

datas = decoder.getBodyHttpDatas(); datas always has a 0 size with PUT, but not with POST.

Thanks for any ideas

1 Answer 1

2

I'm not a CURL expert but maybe CURL is just sending the file "as is" when you PUT rather than as multipart MIME when you POST.

The content type for a PUT is typically the type of the file; e.g. text/xml. For a post, it is multipart/form-data

HttpPostRequestDecoder read and decodes multipart MIME. See comments at https://github.com/netty/netty/blob/master/codec-http/src/main/java/io/netty/handler/codec/http/HttpPostRequestDecoder.java#L187

The end result is that for PUT from CURL check if you can change the content type of multi-part/formdata. However, this is not efficient because of MIME encoding.

I think a better option is to just handle the uploaded data. Basically, when you get the initial HTTP request, open a file. As you receive each chunk, write it to the file. On the last chunk, close the file.

I wrote something a while ago. See https://github.com/chililog/chililog-server/blob/master/src/main/java/org/chililog/server/workbench/ApiRequestHandler.java#L133

Hope this helps.

1
  • Many thanks Veebs, your code on github was very helpful, I ended up creating a custom handler.
    – RandomUser
    Mar 15, 2012 at 17:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.