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 THIS TESTS: 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.
A GOOD ANSWER COVERS: 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.
COMMON WRONG ANSWERS: 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.
LIKELY FOLLOW-UPS: 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.
ONE 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.
Read the original → futurestud.io
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.