Retrofit – Send Data Form-Urlencoded

Retrofit – Send Data Form-Urlencoded

Hi guys, today being Christmass Day I though about having a new article here, and wish you all a fantastic one!

Multiple methods exist to send data to your server. You may already know how to send data within the request body or how to make use of multipart/form-data to upload files using Retrofit.

There’s another type available to send data to an API or server with your request: form-urlencoded. This post shows you how to annotate your service interface and execute form encoded requests using Retrofit.

Form Encoded Requests

Performing form-urlencoded requests using Retrofit is sort of straight forward. It’s just another Retrofit annotation, which will adjust the proper mime type of your request automatically to application/x-www-form-urlencoded. The following interface definitions for Retrofit 2 will show you how to annotate your service interface for form-encoded requests. We’ll describe the magic behind the curtain below the code snippets 🙂

Retrofit 2

We’re using the TaskInterface to illustrate the code. The important part is the @FormUrlEncoded annotation. Please be aware of the fact, that you can’t use this annotation for GET requests.

Form encoded requests are intended to send data to the server!

Additionally, you need to use the @Field annotation for the parameters which you’re going to send with your request. Place your desired key inside the @Field("key") annotation to define the parameter name. Further, add the type of your value as the method parameter. If you don’t use String, Retrofit will create a string value using Java’s String.valueOf(yourObject) method.

Let’s have a look at an example to illustrate all the theory! Using the following service call

results in the following form encoded request body:

Of course, you can have multiple parameters for your request. Just add further @Field annotations with the desired type.

Form Encoded Requests Using an Array of Values

The example above describes the usage of the @Field annotation only for a single value. If you don’t use string as the object type, Retrofit will do the job to parse a string value from the given object.

However, you can pass an array of values using the same key for your form encoded requests.

Retrofit 2

As you can see, the only difference to the previously described service interface is the list of titles as the parameter. Retrofit will map a given list titles to the respective key title.

Alright, time to touch another example to get an impression of how the final request will look like.

results in the following form encoded request body:

Each item within the list of task titles will be mapped to a key-value-pair by Retrofit. The title key will be the same for every pair. The key-value-pairs are separated by an ampersand (&) and the values are separated from their keys by an equal symbol =.

Field Options

The @Field annotation has an option field for encoding:

  • encoded: can be either true or false; default is false

The encoded option defines whether your provided key-value-pair is already url encoded. To specify the encoded option value, you need to pass it within the @Field annotation. The example below illustrates a code examples and sets the encoded option to true.

Form-Urlencoded vs. Query Parameter

When seeing the results for a form encoded request, you may ask yourself “What is the difference between form-urlencoded and query parameters?”. In essence: the request type.

  • form-urlencoded: POST
  • query parameter: GET

Use form-urlencoded requests to send data to a server or API. The data is sent within the request body and not as an url parameter.

Query parameters are used when requesting data from an API or server using specific fields or filter.


Using form encoded request is a convenient way sending data to an API or backend. There’s no data object required to map the information into JSON representation. I have guided you through the annotations required to execute a form encoded request and also showed you how to pass multiple values for the same key with the request url.

Besides that, you can specifically set the encoded option for your values. If you previously weren’t sure about the difference between query parameter and form encoded requests, you now know when to apply the appropriate one.

If you run into questions or problems, leave your questions in the comment section below.

Happy Coding!

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.