Skip to content
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

Found problems posting multipart/form-data with file #1130

Closed
Carlos77c opened this issue Sep 27, 2015 · 1 comment
Closed

Found problems posting multipart/form-data with file #1130

Carlos77c opened this issue Sep 27, 2015 · 1 comment

Comments

@Carlos77c
Copy link

Maybe is something i'm doing while forming the request but just wanted to share my experience implementing retrofit with our api.
First of all i'm using the source project from here last updated yesterday.
This is the api interface call:

@Multipart
@Headers({
    "Accept: application/json",
})
@POST("/myendpoint/")
Call<Void> createMoment(
        @Part("file") RequestBody file,
        @Part("text1") RequestBody text1,
        @Part("text2") RequestBody text2,
        @Part("text3") RequestBody text3,
        @Part("text4") RequestBody text4,
        @Part("text5") RequestBody text5,
        @Part("text6") RequestBody text6
);

How i created the RequestBody for the file and texts in first instance:

RequestBody file = RequestBody.create(mimeType, file);
RequestBody text = RequestBody.create(null, "text"); //For each text part

Doing it this way is our server response was "Missing boundary in multipart/form-data POST data in Unknown on line 0". Firstly i thought that adding it to Headers annotation in the interface call would solve this but our server kept giving the same response. Finally adding these lines just before the return to RequestFactoryParser class(Line 194) solved the boundary issue

if(isMultipart) 
    builder.add("Content-Type", "multipart/form-data; charset=utf-8; boundary=\"myboundary\"");

Once i had the boundary in my request the next problem was the file part, our Api is coded in PHP and uses the tmp_name from $_FILE[] but creating the RequestBody to upload a file with the rest of the data wasn't adding the filename var to the Content-Type header of this request.
I didn't find a way to check mime-type of Part in parseParameters(Retrofit retrofit) method inside RequestFactoryParser class so inside RequestBuilderAction.Part, RequestBuilderAction (Line 211) i check if mime type is an image, the only files we accept so far, and update headers accordingly if it is.
I just added this,

if("image".equalsIgnoreCase(body.contentType().type())){
    headers = com.squareup.okhttp.Headers.of(
        "Content-Disposition", headers.get("Content-Disposition").concat("; filename=\"filename.img\""));
}

and finally got it working.

So my question is, i'm i missing something? This is the first time i use retrofit and okhttp and probably, what i want to achieve, send an image and other plain text data in the same request, has to be done other ways but i haven't found any. I also tried to create each RequestBody with MultipartBuilder().build() and other things, like creating just a single RequestBody for all the data, with no luck. The only thing that has worked for me so far is this one.

@JakeWharton
Copy link
Collaborator

This sounds like #1140, where the filename isn't being added which is breaking the backend. If you can demonstrate otherwise (like with a failing test, ideally) then I'll re-open. But once #1140 is fixed this should just start working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants