-
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
Add a way to set filename on multipart requests #1140
Comments
Hi @lukaciko I had the same problem but finally I found the solution of this bug. I just had to add the name of the field in @part() RequestBody. This is an example: picture - is the name of the parameter and after put the "filename" parameter with any name. Note: Do not forget scape quotation marks :) |
That may work, but it seems a bit hacky. Inherent support would seem more natural. Should this be an OkHttp issue rather than a Retrofit one? Maybe the |
Injecting the filename can be used as a workaround, but it's not exactly pretty. It'd be better if it could be defined with an optional annotation parameter (i.e. @JakeWharton you've advocated using |
Sorry I haven't been too involved here. There is a lot going on in this Most of what you describe has nothing to do with Retrofit, but OkHttp. I would encourage you to experiment with it and see possible ways forward On Wed, Sep 30, 2015 at 3:40 AM Luka Cindro [email protected]
|
I have looked at the source of Retrofit and OkHttp and without changing RequestBody implementation/usage it seems a good way to go is to add some annotation as @partfile("file") with a File object, retrofit could use it to set the "filename" header and to create the RequestBody object. |
@BugsBunnyBR Yeah, that seems reasonable. I'll start thinking about this. It will be resolved with a sample before 2.0 is released. |
@JakeWharton Bugs and I made an attempt in #1188 if that's of any value. |
It's a possible way to support file name rather than (@part("picture"; filename="1.jpg" ") ? I look not such pretty but error-prone. |
@atanl That's what #1188 does. It would look something like this: File file = /* ... */;
TypedFile image = new TypedFile("image/png", file);
// ...
@PartFile("image") TypedFile image The file name would default to the result of |
Using beta.2 public interface UploadService {
@Multipart
@PUT("/somepath/upload")
Call<ResponseBody> upload(@Part("uploadFile") RequestBody upload);
}
UploadService service = retrofit.create(UploadService.class);
File file = new File(filePath);
RequestBody requestBody = RequestBody.create(MediaType.parse(mimeType), file);
Response<ResponseBody> response = service.upload(requestBody).execute(); I would expect headers to look something like this:
Attached a log interceptor to OkHttp and am only seeing these headers:
Is multipart file upload still broken? |
Does indeed work. However, from what I can tell there is no way to set a dynamic filename. IE I don't want to filename to be the same for every upload. When removing the 'filename' portion from the annotation the upload does not work. |
@ayon115 found a good workaround for dynamic filename here: #1063 (comment) |
Btw guys, even with all this solutions I have next problem. @Multipart
@PUT("users/{id}")
Call<JsonObject> uploadInsuranceCardImage(@Path("id") String id,
@Part("profile\"; filename=\"image.jpg\"") RequestBody file); When I do logging I get: Content-Disposition: form-data; name="profile"; filename="image.jpg""
D/OkHttp﹕ Content-Transfer-Encoding: binary
D/OkHttp﹕ Content-Type: image/jpeg
D/OkHttp﹕ Content-Length: 58077 Notice on the end of filename double quotes. |
So the solution to this actually going to mostly live in OkHttp as a model representing both an entire multipart body and its parts (square/okhttp#2082). This will allow a |
@JakeWharton Has this been integrated into beta3? |
No On Fri, Jan 8, 2016 at 3:36 PM Shubhadeep Chaudhuri <
|
Just want to re-iterate comment from @UMFsimke Many people have been repeating this workaround, but this does not work with all servers:
This is wrong; it generates an extra quote. Should be:
Formatted this way, the workaround does work for me. |
@JakeWharton Has this been integrated into beta4? |
No On Thu, Feb 11, 2016 at 1:11 AM Shubhadeep Chaudhuri <
|
It would be ever corrected? ;-) |
no |
Really?? It is time to start learning Volley?? |
No, it's time to start contributing and helping resolve the issue if it is that much of a problem for you. There is a simple documented workaround so I don't know why it would be though. By the way, volley is more analogous to okhttp, not retrofit, so not really appropriate. |
I don't find workaround what is working. There are a lot bad solution here and stackoverflow. Can you show me the working version? I would be check . |
@mesterj Looking at the first post, there are many links to other discussions about this. I am using this and it is working great. |
@mesterj Its working great for me also https://github.com/square/retrofit/issues/1063 |
Thank you. I found two workaround and I made my third which is really works. :-) |
@mesterj Care to share? |
In the API interface:
|
Using a map works (and may be the best option for beta versions of Retrofit 2 before they brought in OkHttp 3), but I believe they promoted Interface: @Multipart
@POST("upload")
Call<UploadResponse> uploadImage(@Part MultipartBody.Part filePart) Client: RequestBody fileReqBody = RequestBody.create(mediaType, file);
MultipartBody.Part filePart =
MultipartBody.Part.createFormData(partName, file.getName(), fileReqBody);
Call<UploadResponse> call = api.uploadImage(filePart); |
MultipartBody.Part is not for @Body parameters but for @part ones On Tue, Jun 7, 2016 at 1:49 PM Greg Williams [email protected]
|
Thank you. It is much better. |
This is my code. This works..
|
WTH is observation_id in your code? |
i am also using a PartMap, and kept getting 500 internal server error when i would add an image to the multipart request. i just tried out what @sbljayarathna did, and it worked for me, too. (thanks!) |
I am trying to upload a pdf file but I keep getting unprocessable entity |
@UMFsimke what does you mean"Notice on the end of filename double quotes. |
This is just a *hack*. but it works. square/retrofit#1140
This is just a *hack*. but it works. square/retrofit#1140
This has already been discussed in #1063 #1092 #1096 and #1130, but I thought I'd open a separate issue to track this specifically.
RFC2388 describes the
filename
header parameter ofmultipart/form-data
requests that Retrofit now uses. This parameter is required on some backend configurations. More details and examples can be found here.We need a way of figuring out what the filename is. The problem with
RequestBody
(which is a part of OkHttp) is that it doesn't expose the filename when created with aFile
.The text was updated successfully, but these errors were encountered: