How to make a multipart request with dynamic URL using Retrofit

Updated 12 August 2017

Save

Nowadays retrofit is a popular library to make HTTP requests. It is simple, fast, and efficient. In this blog, we will learn about making a multipart request using the retrofit.

For this, you need to first define the request params and headers as per your requirement in your Interface class with all other requests. There can be two cases for setting the request URL. It could be a constant or a dynamic URL. We will see how to request for a dynamic URL means the URL which is created at the runtime.

As you can see in the above code segment, we have used two annotations at the top Multipart and Post. They define that the request will be Multipart and it will be a post request.

In the parameters section, We have defined a URL type parameter which will be used as the URL for this request. We have also defined headers and part type params. in Headers, we will define all the required headers and params will be the other data that you are sending using the request.

 

Now in your class, you will have to create the Request bodies which will send as the parameters.

here we have created three RequestBody Object. You need to define the mime-type of each parameter according to the file type. Here we have set the mime type to image/* for a image type file and text/plain for text type object.

 

After creating your RequestBody object now you need to call the request function from your interface class.

The above code segment describes how to call the function and pass your parameters to it. Here this will define that the retrofit call back will be the activity in which the request as been made.

If you are looking for some help with RetrofitClinet. You can follow our previous blog on this.

 

That’s all you need to create a multipart HTTP request using Retrofit library.

Thank you very much. This is Vedesh Kumar signing off.

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


2 comments

  • Laura
    • Vedesh Kumar (Moderator)
  • Start a Project


      Message Sent!

      If you have more details or questions, you can reply to the received confirmation email.

      Back to Home