Upload binary file with retrofit 2 in Android
Just adding another solution, as I had to dig a bit to understand what happened in the question in the first place.
My solution is to get the binary file as a byte[]
directly and then put it inside the RequestBody
. So in the end, the code would look like this:
interface RetrofitService {
@POST("api/v1/upload_file")
Call<Void> uploadBinaryFile(@Body RequestBody body);
}
And to call it:
public void uploadBinaryFile(File fileToUpload) {
retrofitService
.uploadBinaryFile(RequestBody.create(MediaType.parse("application/octet"),
Files.readAllBytes(fileToUpload));
}
This is basically the same as OP's original question, but just for the sake of clarity I'll leave this answer as well for the next reader.
After hours of searching I found that there was an @Multipart
annotation remains in API interface of my code from last example! which prevent to send binary data to the server and the solution in retrofit repository was OK!
I had same problem, I wanted to upload binary file(image). The API was of Wordpress
I followed the solution code which is given at the end of this issue
Here is my little modified code
@POST("wp-json/wp/v2/media/")
Call<ImagePostResult> postEventPhoto(
@Header("Authorization") String accessToken,
@Header("Content-Type") String contentType,
@Header("Content-Disposition") String contentDisposition,
@Body RequestBody photo);
Here is the request
// For BasicAuth
String authHeader = getAuthHeader();
String contentType = "application/binary";
String contentDisposition = "attachment; filename = " + fileName;
RequestBody requestBodyee = null;
try {
InputStream in = new FileInputStream(file);
byte[] buf;
buf = new byte[in.available()];
while (in.read(buf) != -1) ;
requestBodyee = RequestBody
.create(MediaType.parse("application/octet-stream"), buf);
} catch (IOException e) {
e.printStackTrace();
}
Call<ImagePostResult> imagePostResultCall = apiInterface.postEventPhoto(
authHeader,
contentType,
contentDisposition,
requestBodyee);
imagePostResultCall.enqueue(new Callback<ImagePostResult>() {
@Override
public void onResponse(Call<ImagePostResult> call, Response<ImagePostResult> response) {
// Response Success
if (response.isSuccessful()) {
// yaay
}
}
@Override
public void onFailure(Call<ImagePostResult> call, Throwable t) {
Log.d(TAG, "onFailure: " + t);
}
});
In Kotlin you can do this:
val file: File = retrieveMyJavaFile()
val requestBody: RequestBody = file.asRequestBody("application/octet-stream".toMediaTypeOrNull())
val response: MyResponse = myAPI.uploadPhoto(requestBody).body()!!