Retrofit — Send Data Form-Urlencoded

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. If not and you’re interested, follow the lead :) 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.


Retrofit Series Overview

  1. Getting Started and Creating an Android Client
  2. Retrofit 2 — Basics of API Description
  3. Retrofit 2 — Creating a Sustainable Android Client
  4. Synchronous and Asynchronous Requests
  5. Send Objects in Request Body
  6. Add Custom Request Header
  7. Retrofit 2 — Manage Request Headers in OkHttp Interceptor
  8. Retrofit 2 — Dynamic Request Headers with @HeaderMap
  9. Multiple Query Parameters of Same Name
  10. Optional Query Parameters
  11. Using the Log Level to Debug Requests
  12. Retrofit 2 — Log Requests and Responses
  13. Retrofit 2 — Enable Logging for Development Builds Only
  14. How to Upload Files to Server
  15. Retrofit 2 — How to Upload Files to Server
  16. Retrofit 2 — How to Upload Multiple Files to Server
  17. Retrofit 2 — Passing Multiple Parts Along a File with @PartMap
  18. Basic Authentication on Android
  19. Token Authentication on Android
  20. OAuth on Android
  21. Retrofit 2 — Hawk Authentication on Android
  22. How to Refresh an Access Token
  23. Retrofit 2 — Upgrade Guide from 1.9
  24. Retrofit 2 — Simple Error Handling
  25. How to use OkHttp 3 with Retrofit 1
  26. Send Data Form-Urlencoded
  27. Send Data Form-Urlencoded Using FieldMap
  28. Retrofit 2 — How to Add Query Parameters to Every Request
  29. Retrofit 2 — Add Multiple Query Parameter With QueryMap
  30. Retrofit 2 — How to Use Dynamic Urls for Requests
  31. Retrofit 2 — URL Handling, Resolution and Parsing
  32. Retrofit 2 — Constant, Default and Logic Values for POST and PUT Requests
  33. Retrofit 2 — How to Download Files from Server
  34. Retrofit 2 — Cancel Requests
  35. Retrofit 2 — Reuse and Analyze Requests
  36. Retrofit 2 — How to Change API Base Url at Runtime
  37. Optional Path Parameters
  38. Retrofit 2 — How to Send Plain Text Request Body
  39. Retrofit 2 — Pagination Using Query Parameter
  40. Retrofit 2 — Pagination Using Link Header and Dynamic Urls (Like GitHub)
  41. Retrofit 2 — Pagination Using Range Header Fields (Like Heroku)
  42. How to Integrate XML Converter
  43. Define a Custom Response Converter
  44. Retrofit 2 — Introduction to (Multiple) Converters
  45. Retrofit 2 — Adding & Customizing the Gson Converter
  46. Retrofit 2 — Implementing Custom Converters
  47. Retrofit 2 — Basics of Mocking Server Responses
  48. Retrofit 2 — Customizing Network Behavior of Mocked Server Responses (soon)
  49. Java Basics for Retrofit — Callbacks (soon)
  50. Java Basics for Retrofit — Annotations (soon)
  51. Java Basics for Retrofit — Fluent Interface with Builders (soon)

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 1 and 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 1.9

public interface TaskService {  
    @FormUrlEncoded
    @POST("/tasks")
    void createTask(@Field("title") String title, Callback<Task> cb);
}

Retrofit 2

public interface TaskService {  
    @FormUrlEncoded
    @POST("tasks")
    Call<Task> createTask(@Field("title") String title);
}

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

service.createTask("Research Retrofit form encoded requests");  

results in the following form encoded request body:

title=Research+Retrofit+form+encoded+requests  

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 1.9

public interface TaskService {  
    @FormUrlEncoded
    @POST("/tasks")
    void createTasks(@Field("title") List<String> titles, Callback<List<Task>> cb);
}

Retrofit 2

public interface TaskService {  
    @FormUrlEncoded
    @POST("tasks")
    Call<List<Task>> createTasks(@Field("title") List<String> titles);
}

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.

List<String> titles = new ArrayList<>();  
titles.add("Research Retrofit");  
titles.add("Retrofit Form Encoded")

service.createTasks(titles);  

results in the following form encoded request body:

title=Research+Retrofit&title=Retrofit+Form+Encoded  

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.

@Field(value = "title", encoded = true) String title

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.

Outlook

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. We’ve 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.


Explore the Library

Find interesting tutorials and solutions for your problems.

Miscellaneous