question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add a way to set filename on multipart requests

See original GitHub issue

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 of multipart/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 a File.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:37 (8 by maintainers)

github_iconTop GitHub Comments

20reactions
wKovacs64commented, Apr 25, 2016

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 Multipart for exactly this scenario. The current intended way to upload a file while specifying a filename (afaict) is using Multipart.Part. Something like this:

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);
6reactions
sbljayarathnacommented, Jul 8, 2016

This is my code. This works…

public interface FileUploadService {  
    @Multipart
    @POST("upload")
    Call<ResponseBody> upload(@PartMap Map<String, RequestBody> params);
}

 private void uploadFile() {  
        FileUploadService service =
                ServiceGenerator.createService(FileUploadService.class);

        File file = new File(selectedImagePath);
image_name = file.getName();
String fileName = "file\"; filename=\"" + image_name;
RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);

 RequestBody id = RequestBody.create(MediaType.parse("multipart/form-data"), observation_id);

Map<String, RequestBody> requestBodyMap = new HashMap<>();
 requestBodyMap.put("id", id);
requestBodyMap.put(fileName, requestBody);


        Call<ResponseBody> call = service.upload(requestBodyMap);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call,
                                   Response<ResponseBody> response) {
                Log.v("Upload", "success");
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.e("Upload error:", t.getMessage());
            }
        });
    }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Set name header of multipart-encoded file POST
The name is set from the key in your files dictionary. Just alter that key to set the name field to the desired...
Read more >
How to send payload as a multipart/form File in a HTTP Request
You'll need to use a DataWeave script for the body of your request, similar to: %dw 2.0 import dw::module::Multipart input payload application/xml output ......
Read more >
Using FormData Objects - Web APIs | MDN
The FormData object lets you compile a set of key/value pairs to send using XMLHttpRequest. It is primarily intended for use in sending...
Read more >
Request Files - FastAPI
To receive uploaded files, first install python-multipart . E.g. pip install python-multipart . This is because uploaded files are sent as "form data"....
Read more >
Upload files in ASP.NET Core - Microsoft Learn
The file is received from a multipart request and directly processed or saved by ... The following code removes the path from the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found