-
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
Introduce @PartFile for including TypedFile parts in a multi-part request #1188
Conversation
@wKovacs64 , very nice! |
Retrofit is not an Android library and cannot depend on its APIs. But On Fri, Oct 9, 2015 at 3:38 PM Júlio Cesar Bueno Cotta <
|
yet, it may be a good thing to be a dynamic detection. |
If the type is not known explicitly, the caller can dynamically detect On Fri, Oct 9, 2015 at 3:40 PM Júlio Cesar Bueno Cotta <
|
I tried using |
As far as I could check in the PR, I would need an interface method to each contentType I would want to support, right? |
As it stands currently, yes. Open to suggestions on an alternative approach, though. |
I miss TypedFile. |
Are you suggesting replacing |
I think the new TypedFile could have at least 2 constructor methods..
|
Any reason to include the encoding, or can we force |
don't know doc, @JakeWharton ? |
How about just use the okhttp MultipartRequestBuilder ? |
Something like this, if we pursue the wrapper idea? public final class TypedFile {
private final MediaType mediaType;
private final File file;
private final String fileName;
public TypedFile(MediaType mediaType, File file) {
this(mediaType, file, file != null ? file.getName() : null);
}
public TypedFile(MediaType mediaType, File file, String fileName) {
this.mediaType = checkNotNull(mediaType, "MediaType cannot be null.");
this.file = checkNotNull(file, "File cannot be null.");
this.fileName = checkNotNull(fileName, "Filename cannot be null.");
}
public MediaType mediaType() {
return mediaType;
}
public File file() {
return file;
}
public String fileName() {
return fileName;
}
} If we go this route, I guess we could just use |
Yes, try to reuse the second constructor method to set the values in the first constructor method. Something like this(type,file, file.getname()); |
Yeah, sure. Mostly wondering about the |
@BugsBunnyBR What about this? https://github.com/wKovacs64/retrofit/commit/76433edb2f9cdca446f1000dd013f2aad3c20157 I'm still wrestling with the feeling we could/should use |
To me it is fine in this way, but someone of square could say more what is the best strategy here. Let's wait. |
@JakeWharton , could you share your thoughts about this pull request ideas? |
@wKovacs64 How were you thinking of using @part with converter for TypedFile? I tried doing this but ran into problems when it came to actually editing the Header like you did in @partfile RequestBuilderAction. Perhaps I am missing something or do not know enough about the project but I could not figure out where to edit the header. Which means I was still left with the header injection in the @part parameter. |
@px-amaac I tried it briefly and couldn't get it either. If it came down to that being the more correct approach than the current state of the PR, I would need help from Jake/Jesse/whoever. |
Rebased and squashed. |
Use @partfile to include TypedFile parts in a multi-part request.
OkHttp has a Thanks for taking the time to push this forward though. |
Sounds good, will wait for that then. Thanks. |
Since the removal of
TypedFile
in Retrofit 2.x, it has been somewhat of a challenge trying to figure out how to include aFile
part in a multi-part request. @JakeWharton has recommended usingRequestBody
but it doesn't include thefilename
property when created using aFile
, which some backends require. See #1140 for more information and discussion.I've personally been using
@PartMap
, similar to @ayon115's comment in #1063, but @BugsBunnyBR suggested a@PartFile
annotation which sounded like a nice convenience. I took a stab at it here.It may need some help.
Example usage (added 2015-12-01):
Resulting request headers:
Note: the
filename
value defaults to the result ofFile#getName()
but you can override it when creating theTypedFile
if desired: