Search code examples
javaformsswaggermultipartundertow

Multipart Fileupload Formdata attachment is null


Im using a swagger-generated undertow server (light4j) and am trying to achieve a fileupload via html form. Problem is that the Formdata, which is supposed to have the file in it, is null. Code is very simple, what could be the issue here? The examples i found show exactly this code, maybe registered as handler, but this should not affect the functionality. is there anything else to consider?

Frontend

<form action="http://localhost:8081/edit/upload" method="post" enctype="multipart/form-data"> <input type="file" name="upfile" id="upfile"> <input type="submit" value="Upload"> </form>

Backend

@Override public void handleRequest(HttpServerExchange exchange) throws Exception { //following attachment is null! FormData attachment = exchange.getAttachment(FormDataParser.FORM_DATA);


Solution

  • You need to tell undertow to parse the form data. For that you can use the handler EagerFormParsingHandler, as follows:

    Handler h = new EagerFormParsingHandler(yourHandler);
    

    And then indeed, in your handler you retrieve the FormData attachment.