Reputation: 2692
I have to send a post request in this format.
--__X_PAW_BOUNDARY__
Content-Disposition: form-data; name="user_photo[image]"; filename="file.jpg"
Content-Type: image/jpeg
ÿØÿàJFIFHHÿáLExifMM*i
ÿí8Photoshop 3.08BIM8BIM%ÔÙ²é ìøB~ÿÀ
"ÿÄ
ÿĵ}!1AQa"q2¡#B±ÁRÑð$3br
%&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖ×ØÙÚáâãäåæçèéêñòóôõö÷øùúÿÄ
ÿĵw!1AQaq"2B¡±Á #3RðbrÑ
$4á%ñ&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖ×ØÙÚâãäåæçèéêòóôõö÷øùúÿÛC ÿÛC ÿÝZÿÚ?ü_¢+þæð¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¯ë3þ
I am basically trying to send a photo in post request with name user_photo[image]
like the one on the image shown below:
Here are snippets of the solutions I have considered:
Call<models.UploadResponse> uploadPhoto(@Path("id") int userId, @Part MultipartBody.Part file);
MultipartBody.Part body = MultipartBody.Part.createFormData("user_photo[image]", file.getName(), requestFile);
Call<models.UploadResponse> call = userRequest.uploadPhoto(62, body);
RequestBody requestFile =
RequestBody.create(MediaType.parse("image/*"), FileUtils.getFile(PhotoUploadActivity.this, fileUri));
Call<models.UploadResponse> call = userRequest.uploadPhoto(62, requestFile);
Call<models.UploadResponse> uploadPhoto(@Path("id") int userId, @Part ("name=\"user_photo[image]\"") RequestBody file);
@Multipart
@POST("users/{id}/user_photos")
Call<models.UploadResponse> uploadPhoto(@Path("id") int userId, @Part("name=\"user_photo[image]\"") RequestBody file);
This is the okhttp log:
D/OkHttp: Content-Type: multipart/form-data; boundary=75e8ae2e-a160-413f-82d1-3afd94f22c43
D/OkHttp: Content-Length: 43706
D/OkHttp: ������JFIF�����������������Photoshop 3.0��8BIM����������hg��vdP4uk_QnzZ02cW1f845(��JFBMD0f00078d0300006c1d0000675000009d53000068590000c77e000041a90000b1a90000��ICC_PROFILE������lcms����mntrRGB XYZ ���������)��9acspAPPL�����������������������������������������������������������������-lcms����������������������������������������������������������������������������������������������
D/OkHttp: desc�������������^cprt����\������wtpt����h������bkpt����|������rXYZ�����������gXYZ�����������bXYZ�����������rTRC�����������@gTRC�����������@bTRC�����������@desc��������������c2��������������������������������������������������������������������������������������������������������������������������������������������������������������������text��������FB����XYZ �������������������������-XYZ ����������������3�����XYZ ������������o�����8������XYZ ������������b������������XYZ ������������$������������curv����������������������c�k�?Q4!�)�2;�FQw]�kpz���|�i�}���0������C��
D/OkHttp:
D/OkHttp:
D/OkHttp: �"�������������������������������������������������������������������������<r9������J�������@��aCLk@�|2�=�VW�}������E���� �,i�v!�Q;j�H�N@E0!��1�J�T5L4�������� ��h���U��S�м�6v���2��]
D/OkHttp: ����3��T0��-@t�`&h��������%̬�Η�����[w(��جƥ�f^
D/OkHttp: C���;���t������E�͂I�O�[�Hvݝ -�g߫t�k���y����ة�ۼ�6S7zEe��/��I_��b��;�Gr��/�:B��R-7$eO��M��G�۱Iū�G2�r�s7#p�P��Ha�o6U�$�3(�����G\�q�4�ȋ�Dj�S�������n�V���J�f?m{{��ޣq�<\��_L���v+�1��-�\h���\u�Pnms����9 ��".~N�z�*J>f�I�:$��T j�C�{���$�_$3y���ߪy�¼žj���j�愰�s�z�)�o���J����]�˭���ŧ��s]���
D/OkHttp: ��)��(�u�����şLl_T���S��"(��_o�{n�3���}OU�P`}C^zl�]V"}��u�Un|=���ο-�mn�e�1�g���S���Q0ދggF������B�;ӫ>��ӷ�P���5c�x"�#o�9螗�s`�j2����O�Y�ӆa��-�便�zy���d���
D/OkHttp: E�r�jqL�ah�^�u��'�+�Z����N:{a�'N��1���H�M�t �H���i���û�Ԕ�hP
D/OkHttp: ���0���
D/OkHttp: �����Y#M�qg���+��?��T�����ܱ���4�@��CA@����������&����G����wL���<Vo�l�Q�۲����H_?a�I�ũT��z��H���Z����S�b)���d���L11�3������hL �����h����b� ����
D/OkHttp: <Rf"��L��(�
D/OkHttp: s��3�Pgı��`g��ω��>&|J�S%���]�b�m�q#Ө�k��
Also:
Solutions I have referred:
And I am tired of searching for solutions. Can anyone help?
Upvotes: 7
Views: 9807
Reputation: 10971
I used the following methods, they are not in RetroFit, hope that helps: Server Side: Spring Boot java application:
@RequestMapping(value="/applyeffect", method=RequestMethod.POST,produces=MediaType.APPLICATION_OCTET_STREAM_VALUE)
public @ResponseBody byte[] applyEffect(@RequestParam("userid") String userId, @RequestParam("type") int effectType,@RequestParam("file") MultipartFile file){
try {
byte[] bytes = file.getBytes();
File temp=new File(file.getName());
BufferedOutputStream stream =
new BufferedOutputStream(new FileOutputStream(temp));
stream.write(bytes);
stream.close();
File newFile=null;
if(effectType==1){
newFile=ImageUtils.applyTint(userId, temp, 50);
}else if(effectType==2){
newFile=ImageUtils.applyEffect(userId, temp, 128);
}else if(effectType==3){
newFile=ImageUtils.applyBlackWhiteEffect(userId,temp, 128);
}
FileInputStream input=new FileInputStream(newFile);
byte[]data=IOUtils.toByteArray(input);
return data;
} catch (Exception e) {
System.out.println(e.toString());
return null;
}
}
Android Client side: I used this util class https://github.com/MinaSamy/DailySelfie/blob/master/app/src/main/java/bloodstone/dailyselfie/android/utils/PostMultiPart.java
Upvotes: 0
Reputation: 1264
I've used this lib and it works like a charm for uploading multiparts
Upvotes: 2
Reputation: 1830
You have to send it as typed file.
TypedFile upload_file = new TypedFile("multipart/form-data", new File(your_file_location));
@Multipart
@POST("users/{id}/user_photos")
Call<models.UploadResponse> uploadPhoto(@Path("id") int userId, @Part("name=\"user_photo[image]\"")TypedFile upload_file);
Hope it helps
Upvotes: 0
Reputation: 5375
change
@Part("name=\"user_photo[image]\"") RequestBody file)
to
@Part("user_photo[image]\"; filename=\"file.jpg\" ")
Create file's requestBody using
RequestBody fileBody = RequestBody.create(MediaType.parse("image"), myImageFile);
You can find more about this on
https://github.com/square/retrofit/issues/1140
In this case the filename file.jpg
is hardcoded you can even have dynamic filenames. Although I didn't try dynamic file naming but below is the link which might work
https://github.com/square/retrofit/issues/1063#issuecomment-145920568
Upvotes: 1
Reputation: 2970
@Multipart
@POST("users/{id}/user_photos")
Call<SignUpResp> uploadPhoto(@Part("description") RequestBody description, @Part MultipartBody.Part file,(add if more parameter req as like ID,name));
if (fileUpload != null) {
/**
* code for multipart
*/
// create RequestBody instance from file
RequestBody requestFile =
RequestBody.create(MediaType.parse("multipart/form-data"), fileUpload);
// MultipartBody.Part is used to send also the actual file name
MultipartBody.Part body =
MultipartBody.Part.createFormData("user_photo[image]", fileUpload.getName(), requestFile);
// add another part within the multipart request
String descriptionString = "hello, this is description speaking";
RequestBody description =
RequestBody.create(
MediaType.parse("multipart/form-data"), descriptionString);
call = userRequest.uploadPhoto(description, body, authData);
}
Upvotes: 8