Search code examples
jodd

How to upload multiple files with jodd-http?


How do I upload muliple files in the same request using jodd HTTP?

I've tried something like the following but only the first file was posted.

HttpRequest httpRequest = HttpRequest
        .post("http://srv:8080/api/dlapp/add-file-entry")
        .form(
            "title", "test",
            "description", "Upload test",
            "file", new File("d:\\a.jpg.zip"),
            "file", new File("d:\\b.jpg.zip")
        );

    HttpResponse httpResponse = httpRequest.send();

Solution

  • That is the correct code. You simply add file parameters:

    HttpRequest httpRequest = HttpRequest.post("localhost:8173/echo")
            .form(
                "title", "test",
                "description", "Upload test",
                "file1", temp1,
                "file2", temp2
            );
    

    Nothing more, nothing less. There is the testcase that checks just that.

    The easiest way to check is to fireup e.g. Wireshark on your local machine and simply inspect the request; there must be two files blocks in it.

    Is it possible that your serverside, for some reason is not accepting files?

    Do you use the latest version (v5.0.x)?

    p.s. if you are sending two files, please use two different parameter names (e.g. file1, file2).