How do you upload a file with text data in Retrofit?

Tests multipart uploads versus JSON bodies. Strong answer: @Multipart, @POST, @Part; file as MultipartBody.Part and text as RequestBody; avoid @Field. Red flag: suggesting @Body with a data class.
What's really being asked
Whether you understand how Retrofit 2 sends mixed binary and text payloads using multipart form-data. Interviewers want to see that you distinguish between JSON request bodies and file uploads, and that you can name the exact annotations and types required without guessing.
The full answer
First, annotate the interface method with @POST and @Multipart. The @Multipart annotation tells Retrofit to build a multipart form-data request body. Second, the file parameter must be declared as @Part MultipartBody.Part. You create this by wrapping a File in a RequestBody with the correct media type such as image/jpeg, then passing it to MultipartBody.Part.createFormData along with the form field name and optional filename. Third, text fields are passed as @Part RequestBody or directly as @Part String if no special media type is needed. Fourth, note that you should not manually set the Content-Type header to multipart/form-data because Retrofit adds it automatically along with the boundary when @Multipart is present. Fifth, mention that each @Part takes a name string that must match what the backend expects.
The mistakes people make
Using @Field or @FormUrlEncoded instead of @Part and @Multipart. Form-urlencoded data cannot carry binary file streams. Proposing a single @Body with a Kotlin data class; that sends application/json and will fail when the server expects multipart. Passing a raw File object without wrapping it in RequestBody and MultipartBody.Part. Forgetting the @Multipart annotation, which causes the request to omit the required boundary and content type headers.
What usually comes next
How would you upload multiple files at once? Expect the candidate to suggest multiple @Part parameters or a @PartMap. How do you track upload progress? Expect an answer about a custom RequestBody that overrides writeTo and reports bytes written, or an OkHttp network interceptor. What if the server requires a specific filename in the multipart payload? Expect MultipartBody.Part.createFormData with an explicit filename argument rather than relying on the file name property.
A concrete example
The interface declares @Multipart @POST profile upload fun uploadProfile @Part image MultipartBody.Part @Part userId RequestBody Call ResponseBody. To call it, create a File from the local path, wrap it with file.asRequestBody passing image/jpeg, then build MultipartBody.Part.createFormData with the server field name profile image, the filename, and the request body. For the user ID, convert the string with userId.toRequestBody using text/plain. Pass both parts to the method and enqueue or await the call.
Interview question
When uploading a JPEG image and a user ID string via Retrofit 2, which interface definition follows the correct multipart pattern?
- a.Use @POST with @Body containing a data class that has File and String properties
- b.Use @FormUrlEncoded and @POST with @Field for the file path and user ID
- c.Use @Multipart and @POST with @Part MultipartBody.Part for the file and @Part RequestBody for the textCorrect
- d.Use @Multipart and @POST with @Part File for the image and @Field for the user ID
Why? this is the answer
Option C is correct because @Multipart tells Retrofit to build a multipart/form-data body with automatic boundaries, MultipartBody.Part carries the binary file, and RequestBody handles text fields. Option D is tempting because it uses @Multipart, but passing a raw File instead of wrapping it in RequestBody and MultipartBody.Part will fail, and @Field cannot be used inside a multipart request.
Just read this? Test yourself on what you have been reading.
Read the original → futurestud.io
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on android — each one lists the topics its interview covers.
See open roles