0

I am executing a post type request using Multipart. The problem is because I keep getting two errors

1) 500

2) 422 Unprocessable Entity

Everything works in the postman enter image description here

Api only accepts music files.So I added a default file so as not to constantly choose a new one

RequestBody body = 
             RequestBody.create(MediaType.parse("audio/type"),file);
MultipartBody.Builder builder = new 
MultipartBody.Builder().setType(MultipartBody.FORM);
builder.addPart(body);
GeoService.saveSound(builder.build(), SoundResponseCallback,  
getAuthToken());

and my interface which

@Multipart
@POST("audios")
Call<SoundResponse> saveSound(
          @Part("audio[file] ; filename=audio.mp3")RequestBody file,
          @Query("auth_token") String authToken);

I would appreciate any help.

and I found it Send file to server via retrofit2 as object

3 Answers3

0

You must send MultipartBody.Part type of parameter in your API

Try this:

@Multipart
@POST("audios")
Call<SoundResponse> saveSound(
      @Part MultipartBody.Part file,
      @Query("auth_token") String authToken);
SebastienRieu
  • 1,443
  • 2
  • 10
  • 20
0

Change this

@Part("audio[file] ; filename=audio.mp3")RequestBody file,

to

@Part MultipartBody.Part audioFile,

Also make sure that you have enabled the necessary READ WRITE storage permissions

Manoj Perumarath
  • 9,337
  • 8
  • 56
  • 77
0
@Multipart
@POST("audios")
Call<SoundResponse> saveSound(
         @Part MultipartBody.Part audioFile,
          @Query("auth_token") String authToken);

and

MultipartBody.Part body = MultipartBody.Part.createFormData("audio/type", file);  
 GeoService.saveSound(body, SoundResponseCallback,  
    getAuthToken());
YuvrajsinhJadeja
  • 1,383
  • 7
  • 23