Request MultiPart/Form-Data with ByteArray

Viewed 2859

i want to send a request includes bytearray as form-data. Everyone using "File" but i have just the "bytearray" and i don't want to use any path. My request in curl:

curl -F file=@file server

In java what i tried:

byte[] fileByte = Base64.decodeBase64(parameter);
ByteArrayInputStream myFile = new ByteArrayInputStream(fileByte);
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("server");

multipartEntityBuilder.addBinaryBody("file", myFile, ContentType.APPLICATION_OCTET_STREAM, "filename");

HttpEntity multipart = multipartEntityBuilder.build();
httpPost.setEntity(multipart);

and i tried

multipartEntityBuilder.addBinaryBody("file", fileByte, ContentType.DEFAULT_BINARY, "filename");

//OR FileBody, ByteArrayEntity, InputStream or like thats

So, that methods not worked for me. How i can send the request successfully ?

EDIT: i used the ptsv2 with postman and result -> enter image description here

when i send the same request and file with java the result -> enter image description here

i think issue is certainly related to the ByteArray or InputStream. I must find the another Type for the my byte[] or right method for post in java with using the File type but without path.

1 Answers

I don't know what "server" is, but maybe the problem is with the server you are sending to. I ran your code, writing to a public HTTP POST test server available on the internet, and it works fine for me. Here's the code I ran:

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

class ListsTest {

    public static void main(String[] args) {

        byte[] fileByte = "Sample string data".getBytes(StandardCharsets.UTF_8);

        MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost("https://ptsv2.com/t/r7ypk-1613291354/post");

        multipartEntityBuilder.addBinaryBody("file", fileByte, ContentType.DEFAULT_BINARY, "filename");

        HttpEntity multipart = multipartEntityBuilder.build();
        httpPost.setEntity(multipart);

        try {
            client.execute(httpPost);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

If you go to the target test server bucket:

https://ptsv2.com/t/r7ypk-1613291354

you can see what the full request looks like as received by the server, and see that the payload makes it over just fine as a file with the appropriate filename and contents. The file content portion of the request display looks like this:

enter image description here

Your alternate method worked equally well for me. I chose to post this version as it is simpler, not requiring you to wrap your byte array in an InputStream object.

Related