-
Notifications
You must be signed in to change notification settings - Fork 7.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to Upload a File using Retrofit 2.0 #1063
Comments
Same as v1. Use the Multipart annotation on the method and Part annotation On Mon, Sep 7, 2015, 7:06 AM Vivek Kiran [email protected] wrote:
|
Oh for the file itself, use RequestBody for the type. On Mon, Sep 7, 2015, 10:50 AM Jake Wharton [email protected] wrote:
|
Hi, I am getting following exception while executing:
09-07 17:49:38.072 13562-13582/? W/EGL_emulation﹕ eglSurfaceAttrib not implemented java.lang.IllegalStateException: JSON must start with an array or an object. Everything worked until I migrated to Retrofit 2.0. Is anything wrong with my request? Thanks in advance. |
Retrofit 2 is not released so I'll assume you mean the beta1. This is On Mon, Sep 7, 2015, 6:06 PM Vadim Oleynik [email protected] wrote:
|
Use the latest snapshot for the fix, or wait until the next release. |
Actually, I have downloaded the latest version using:
But issue still exists :( On Mon, Sep 7, 2015 at 7:25 PM, Jake Wharton [email protected]
|
I have also tried the same as VadimWelldone, and experienced no change or fix of the problem. Edit: After some messing around this seems to be a problem with multi parts and not the use of RequestBody's |
Thanks Ashig. I'll try it a little bit later. Hope your approach will On Mon, Sep 14, 2015, 7:30 AM Ashiq Uz Zoha [email protected]
|
Hello @VadimWelldone , I deleted that comment because after spending some more time, I noticed that for doing that approach I get status code 200 as response (may be because of my backend) but those parameters don't actually gets stored to my database correctly. So, thats not actually a solution. But here is my real finding why. I am almost sure that, the Multipart request (file upload) is from okhttp. Retrofit probably has nothing to do with it. My Multipart request has both text and image data. When I pack all my text data into a PartMap and use @PartMap in Retrofit, these things gets Posted to backend correctly. But there's problem with image or file upload. Retrofit 2.0 tells to use okhttp RequestBody to send file. I tried that both with Retrofit and Standalone okhttp library. None of my attempt could successfully upload the file to backend. So, I guess, this problem is from okhttp and we know Retrofit 2.0 uses it as default client. And I confirm that my API is fine as I can POST image and texts in single Multipart request with Android Volley. |
I had problem to upload file using Retrofit 2.0-SNAPSHOT before and found out the multipart format is not compatible with my server which accepts multipart/form-data. Here is my fix : jimxj@b44ab36 |
Still getting Retrofit 1.9.0 (using OkHttp 2.5.0): @Multipart
@POST("/image/upload")
Observable<UploadImageResponse>
uploadImage(@Header(HEADER_AUTH_KEY) String token,
@Part("userId") String userId,
@Part("images") TypedFile file); Where the file parameter is constructed as follows: TypedFile file = new TypedFile("image/*", new File(path)); Retrofit 2.0.0-SNAPSHOT: @Multipart
@POST("image/upload")
Observable<UploadImageResponse>
uploadImage(@Header(HEADER_AUTH_KEY) String token,
@Part("userId") String userId,
@Part("images") RequestBody file); Where the file parameter is constructed as follows: RequestBody file = RequestBody.create(MediaType.parse("image/*"), path); Am I doing something incorrectly, or is this not the same thing that @JakeWharton said was fixed in master? Did you get it to work, @VadimWelldone? I did notice Gson can serialize |
A fixed was recently merged #1092 (comment). Is it already in the snapshot? |
snapshots are automatically deployed On Thu, Sep 24, 2015 at 10:27 PM Tevin Jeffrey [email protected]
|
@wKovacs64 It's very sad but I didn't manage to get it to work. I have rolled back to Retrofit 1.9.0 and continued to use it. Time frames was crucial for me and I chose working solution. |
Possibly related @VadimWelldone issue It seem it was serializing the file as a Form Field instead of a File Field Debugging from a Python(Flask) server showed that print request.form
rather than
|
2.0.0-beta2 got me past the JSON problem (not sure why the snapshot didn't help). Now investigating the lack of Do you have the headers from that request, @waleoyediran? I'm assuming yours must've included the filename somehow if you're seeing it on the web server like that. |
I just tested with the method commented in #1140 and confirm that it works correctly. I am able to upload file in Multipart Request successfully. Sample code is below
"file" within @part annotation is the name of the file parameter and "filename" is the name of the file that's going to be uploaded. I executed the above request like this...
Hope it helps. |
@ayon115 Thank you so much. It really helped. |
@ayon115 Thanks, it works! But how to specify a dynamic filename in the interface (or in the RequestBody)? |
@mengoni , I think we can't specify dynamic filename in the interface right now. As far I know, it's probably okhttp matter. I am using @PartMap to avoid hard-coding filename with Interface method. You can see the following example that might be useful.
I call it by following way.
The method toRequestBody just converts String into RequestBody
|
@ayon115 saved me as well. After upgrading from 1.9, in which I effortlessly uploaded image files with a
It works. This, unfortunately, does not work, as the back end can not find a FILE param named 'image':
|
@mhousser The way forward is being discussed/tracked in #1140. I have a PR in to essentially bring back a version of the old File file = /* ... */;
TypedFile image = new TypedFile("image/png", file);
// ...
@PartFile("image") TypedFile image ...which should look familiar. |
Thanks!
Is pretty hideous, but it works for now. I can hard-code the filename because I don't actually pay attention to it or use it server-side. (I hope this doesn't cause conflicts between different users uploading with the same filename..?) |
Is anybody know how i can show progress when i'm uploading a file? |
Yes, @kafji, I've seen this guide. It is for OKHTTP , not for retrofit. I've already implemented my request using okhttp only but it's not enough. I need to make it with retfit. Nice EXAMPLE android project with all the features implemented would be more that enought for all who struggles. |
My solution for this problem:
Called like this:
|
What worked for me is as follows:
Note that the difference from a few other answers here is filename="image.jpg" without the escape char at the end. |
work like a charm! |
This is my code. This works...
|
This is my code and it's working flawlessly
|
@Tar-SSA How are you able to use |
This is my dependencies related to Retrofit compile 'com.squareup.retrofit2:retrofit:2.0.2' Are they the same with yours? |
@Tar-SSA ah thank you I was still using the 2.0.0-beta version and hence the issue |
I was never work with Jsoup before, and now I have a project, where guys were using JSoup lib, and I need to do some refactoring and make same work but with retrofit2... I stuck with converting request that send image file. Here is original JSoup request:
here is what i try to do with retrofit:
@multipart
|
@Tar-SSA , @sbljayarathna , @lectricas , @markini , @jemshit , @CateyesLin , @DeepakRattan ?? |
This is not a help forum. Try StackOverflow. |
@multipart
I also tried this way but not getting success. Server is asking for "insert your image". please help me |
If i look your code
use and call for file with name I hope work |
Glad to know it helped you :) @jounghu |
@ayon115 , I am having problem uploading a video to server using Retrofit2 multipart. Please could you help? I have posted a question in stackverflow. https://stackoverflow.com/questions/44319052/upload-video-using-retrofit2 |
Thank you, everyone, for your solutions, it helped me a lot and resolved my issues, this issue thread is a great guide for other developers facing similar issues. |
I need to upload file inside a JSON object like below.
How to achieve this via retrofit 2.0. |
How to upload image and content in the same time with retrofit use Oauth 1.0 |
Hello any one can help me to upload audio file using retrofit i am not able to upload audio file and play it |
Thanks
…On Thu, Jan 25, 2018 at 1:31 PM, nicemak ***@***.***> wrote:
I am having problem uploading an image to server using Retrofit2
multipart. Please could you help? I have posted a question in stackverflow.
https://stackoverflow.com/questions/48436618/unable-to-
upload-image-with-retrofit-2
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#1063 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AKq7H_hLPcrTthuXbdSJ79VDvKK4CRGeks5tODTCgaJpZM4F450F>
.
|
@markini thank u |
来自中国的娃娃们,看这里。 @multipart RequestBody requestBody = RequestBody.create(MEDIA_TYPE_AUDIO, audioFile); @markini 这位大佬的方案亲测有效。 |
Just in case anyone struggles with uploading an image via ImageField in Django, |
@rajvijay68 I belive |
How to upload a file using multipart/form-data upload in Retrofit 2.0, I am unable to upload and there is no proper documentation anywhere on uploading file using retrofit 2.0, I followed the docs in retrofit website but I was not able to make it work.
All the examples I see used TypedFile. It is not clear how to use RequestBody. Please post sample code.
The text was updated successfully, but these errors were encountered: