Tuesday 1 December 2020

How to upload images within request body in retrofit?

Server code (C#):

  • Previously
public class RequestDto : DtoBase
{
    public string id { get; set; }

    public List<byte[]> Images { get; set; }

    public decimal Latitude { get; set; }

    public decimal Longitude { get; set; }
}
  • Updated
public class RequestDto : DtoBase
{
    public string id { get; set; }

    public List<IFormFile> Images { get; set; }

    public decimal Latitude { get; set; }

    public decimal Longitude { get; set; }
}
  • API
[HttpPost("dataTransfer")]
public async Task<ResponseDto> DataTransfer(RequestDto dto) {
}

Mobile application (Kotlin):

This was my DTO class previously:

@JsonClass(generateAdapter = true)
class RequestDto(
    val id: String,
    val images: List<ByteArray>,
    val latitude: Double,
    val longitude: Double
)

And my retrofit interface looked like this:

@POST("/{projectId}/dataSync/dataTransfer")
fun transferData(
    @Path("projectId") projectId: String,
    @Body Request: RequestDto
): Deferred<ApiResponse<ResponseDto>>

Now I need to upload a list of image files instead of List<ByteArray>. How do I go about doing that? Kindly point me in the correct direction.

Update:
This is not an issue to be resolved on the client side. Will delete the question once the bounty period ends.



from How to upload images within request body in retrofit?

No comments:

Post a Comment