Retrofit is overriding the Content Type to multiformpartbody/form-data after uploading it as audio/mp3

Viewed 134

I'm uploading a file using Retrofit to AWS S3, however the content-type is being overridden everytime I upload. I have the the CONTENT-TYPE audio/mp3 however the file on S3 is being overridden as content-type multiformpartbody/form-data. What am I doing incorrectly?

        File file = new File(String.valueOf(Uri.parse(selectedImagesList.get(current_image_uploading))));
        ProgressRequestBody requestFile = new ProgressRequestBody(file, "audio/mp3");

        MultipartBody.Part body =
                MultipartBody.Part.createFormData("audio", file.getName(), requestFile);

        RetrofitInterfaces.IUploadMP3 service = RetrofitClientInstance.getRetrofitInstance()
                .create(RetrofitInterfaces.IUploadMP3.class);

        Call<Void> call = service.listRepos(uploadUrls.get(current_image_uploading), body);
1 Answers

Most likely you need to override the header when you send the request. You can either do it for each request:

OkHttpClient.Builder httpClient = new OkHttpClient.Builder();  
httpClient.addInterceptor(new Interceptor() {  
    @Override
    public Response intercept(Interceptor.Chain chain) throws IOException {
        Request original = chain.request();

        Request request = original.newBuilder()
            .header("Content-Type"," audio/mpeg") //Set the content type here
            .method(original.method(), original.body())
            .build();

        return chain.proceed(request);
    }
}

OkHttpClient client = httpClient.build();  
Retrofit retrofit = new Retrofit.Builder()  
    .baseUrl(API_BASE_URL)
    .addConverterFactory(GsonConverterFactory.create())
    .client(client)
    .build();

Or, if you don't want to override every request, you can do a static override for your call like so:

public interface YourService {  
    @Headers("Content-Type: audio/mpeg")
    @GET("/your/path")
    Call<List<Task>> myFunction();
}

Both examples can be found here:

Related