Reputation: 4220
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
Upvotes: 0
Views: 1593
Reputation: 2388
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.
Upvotes: 2