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
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
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);
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
#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());
Related
I want to export my database file and tehn send it to server using retrofit.
Challenges: Cannot find the request body type for db file in retrofit to send.
Use MultiPart like below.
Convert you file to Multipart like
RequestBody requestBody = RequestBody.create(MultipartBody.FORM, dbFile);
MultipartBody.Part bodyPart = MultipartBody.Part.createFormData("file", fileName, requestBody);
Then in your api interface
#Multipart
#POST("database-upload")
Call<ResponseBody> uploadBackup(#Part MultipartBody.Part file);
I am using Retrofit 2.0 for image upload to server in Android. Following is the interface code.
#Headers("Content-Type: multipart/form-data; boundary=MyMediaFormBoundary")
#Multipart
#POST("api/media/upload/v1")
Call<MyResponse> uploadMedia(#Header("authToken") String authToken,
#Part("media_file\"; filename=\"media_file.jpg\" ") RequestBody filePart,
#Part("media_format") RequestBody format,
#Part("media_library_id") RequestBody noteId,
#Part("media_title") RequestBody title);
Here is my code to create request body params
File file = new File(mediaFileUri.getPath());
RequestBody fbody = RequestBody.create(MediaType.parse("image/*"), file);
RequestBody formatBody = RequestBody.create(MediaType.parse("text/plain"), format);
RequestBody titleBody = RequestBody.create(MediaType.parse("text/plain"), title);
RequestBody idBody = RequestBody.create(MediaType.parse("text/plain"), id + "");
When I call my interface method with above parameters, I am getting error 'steam ended unexpectedly' and I am wondering what exactly I am doing wrong.
I also tried using MultipartBody.Part object of retrofit in above interface but still same result.
Following are the links which I used for reference:
first link
second link
Note: I doubt it is related to bad request but I am not getting much help from backend team and it is working on iOS. The only info I have is server side is fetching media file against key 'media_file' but I am wondering where to use it.
If you are sending image then why the media type is text/plain?
I have to upload an image to server using Retrofit2.
Everything has worked fine at first, but after I re-check it before the release the API failed that can't upload the image but still works using postman!
My code is :
MultipartBody.Part imagePart = MultipartBody.Part.createFormData("image",
image.getFilename(), RequestBody.create(MediaType.get(image.getMimeType()), image.getFile()));
RequestBody sessionPart = RequestBody.create(MultipartBody.FORM, sessionToken);
RequestBody userIdPart = RequestBody.create(MultipartBody.FORM, userId);
RequestBody pictureTypePart = RequestBody.create(MultipartBody.FORM, pictureType.pictureTypeValue());
return dubaingNetwork.uploadProfilePic(accessToken,userIdPart,sessionPart,imagePart,pictureTypePart);
My api interface is:
#Multipart
#POST(Urls.UPLOAD_PROFILE_PIC)
Observable<UploadProfilePicResponse> uploadProfilePic(#Header(ACCESS_TOKEN_FIELD_NAME) String accessToken,
#Part(USER_ID_FIELD_NAME) RequestBody userId,
#Part(SESSION_TOKEN_FIELD_NAME) RequestBody sessionToken,
#Part MultipartBody.Part image,
#Part("image_type") RequestBody pictureType);
Edit: I tried to convert it to urlencoded type with the image converted to string Base64 and the parameters as a field but still the same error on phone and in postman still works, so I don't think it's a server-side error
I'm trying to send a request to server with Retrofit 2, one of my parameters should be like this:
[["Ingredient","amountInt","unit"]]
That the inner list is dynamic,and it can be many of these! like below
[["Ingredient1","1","kilo"],["Ingredient2","2","kilo"]]
I've tried to make this arrays with List<String[]> and JSONArray and List<List<String>>
But none of them works and I get time-out from server
I get my request with insomnia and postman , and I get success response
This is my Retrofit Interface
#POST("my server link")
Call<MyResponse> addFood(
#Header("token") String token,
#Part("name") RequestBody getName,
#Part("time") RequestBody getTime,
#Part("description") RequestBody getDescription,
#Part("hardness") RequestBody getHardness,
#Part("group") RequestBody getGroup,
#Part("numberOfPeople") RequestBody getNumberOfPeople,
#Part("tags") RequestBody getTags,
#Part("source") RequestBody getSource,
#Part("ings")List<String[]> ings);
And this is how I creat my ings:
String[] getIngs=new String[3];
ArrayList<String[]> ingsFinal= new ArrayList<>();
getIngs[0] = getIngredient;
getIngs[1] = getAmount;
getIngs[2] = getUnit;
ingsFinal.add(getIngs);
How can I fix this?
I get time-out from server
When server received wrong params it should create respons with some error.
Time-out depends on internet connection or incorrect url.
You should check Retrofit.Builder().baseUrl() and #Post() urls.
baseUrl = "http://url.url/" postUrl="path/path"
I am writing a client-server Android application. I need to send a file created by user (photo) to server via POST request. The problem is, that when i try to send a file, i can't add a POST Field to my request. Maybe I'm wrong fundamentally, and this operation should be done another way?
#FormUrlEncoded
#Multipart
#POST("/answer/add-file")
Call<AbstractServerResponse> sendSingleFile(#Query("access-token") String accessToken,
#Query("w") String screenWidth,
#Field("answer_id") Integer answerId,
#Part("file") File fileToUpload);
When i try to send files in a multipart way only, i get an exception:
java.lang.IllegalStateException: JSON must start with an array or an object.
As I understand, this happends because the body (main part) of the request is empty.
Multipart requests are used when #Multipart is present on the method. Parts are declared using the #Part annotation.
------ from http://square.github.io/retrofit/
I'm using #Multipart in my project like this:
#POST("/{action}")//POST 多个文件
#Multipart
public void PostAPI(
#Path(value = "action", encode = false) String action,
#Part("path") TypedFile[] typedFiles,
#PartMap Map<String, String> params,
Callback<APIResponse> callback);
Maybe u can try this:
#Multipart
#POST("/answer/add-file")
Call<AbstractServerResponse> sendSingleFile(
#Query("access-token") String accessToken,
#Query("w") String screenWidth,
#Part("answer_id") Integer answerId,
#Part("file") File fileToUpload);
You cannot use both #FormUrlEncoded and #Multipart on a single method. An HTTP request can only have one Content-Type and both of those are content types.