Trying to upload a file. I have to use apache-http, because it's used in our codebase.
If I'm providing file as byte[] to builder.addBinaryBody - I'm getting invalid_form_data error in response, with either APPLICATION_OCTET_STREAM or DEFAULT_BINARY content type.
If providing as File - same, invalid_form_data.
If as FileInputStream(file) - getting no_file_data.
public void sendFile(file) throws Exception {
HttpPost request = new HttpPost("https://slack.com/api/files.upload");
request.addHeader("Authorization", "Bearer " + token);
request.addHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.getMimeType());
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addBinaryBody(
"file",
FileUtils.readFileToByteArray(file), // or just file, or new InputFileStream(file)
ContentType.APPLICATION_OCTET_STREAM, // or DEFAULT_BINARY
"file.txt"
);
request.setEntity(builder.build());
HttpResponse response = client.execute(request);
String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println("Response: " + responseString);
// either {"ok":false,"error":"invalid_form_data"} or {"ok":false,"error":"no_file_data"}
}
What am I donig wrong?