ninjato icon indicating copy to clipboard operation
ninjato copied to clipboard

Example of uploading raw binary data file using Ninjato

Open free5ty1e opened this issue 4 years ago • 2 comments

Question

I've been working with Ninjato for non-upload calls for a couple weeks now, and it works great! Very flexible and does everything I've needed with intercepting responses and retry logic and everything.

However, I can't seem to figure out how to construct a MultiPartEntity / MultiPart Body like I've used before in Apache's old HTTP libraries and add it to the Ninjato Body object. I see the okhttp MultiPartBody but I'm not sure a) how to use this with Ninjato's Body object, and b) not sure how to addBinaryPart() like I did in Apache (see below method I am looking for in Ninjato)

public MultipartEntityBuilder addBinaryBody(String name, byte[] b, ContentType contentType, String filename) {
        return this.addPart(name, new ByteArrayBody(b, contentType, filename));
    }

Suggestion (Optional)

If this doesn't already exist in the sample app / sample code or docs, can it be added? I must be missing something.

Environment

I am using version 0.3.1 which is indicated to be the latest version both in the README.md and at maven central. I am working in Kotlin wherever possible but I am interfacing with a fair amount of existing Java as well.

free5ty1e avatar Oct 15 '20 05:10 free5ty1e

Hi there! Unfortunately, multipart form data is not supported out of the box by library at the moment, since this is a rare scenario and no time was put into supporting this feature. You can construct such body manually, or use OkHttp or Apache directly. Or you can try and contribute support of multipart body to the library 👍

I would imagine it looking something like this:

post {
    endpointUrl = ""
    body = multipart { // We already have similar extension for formUrlEncoded
        "part1" to "text"
        "part2" to File("/myFile.txt")
    }
}

I will try to implement something like that myself, but the timeline for that is unclear. Cheers!

Unlimity avatar Oct 15 '20 06:10 Unlimity

Thank you so much! I understand now; I thought I was missing part of the library as I was poking through the available methods and classes. If I get something worthy of contribution together, I'll put together a PR. I love the library, thank you once again for Ninjato!

free5ty1e avatar Oct 15 '20 15:10 free5ty1e