Simple POST request on Android Kotlin using Retrofit

Sazzad Hissain Khan
The Startup
Published in
2 min readApr 1, 2020

This article is for developers who want to query a POST request to server and parse the response into a custom object in Android Kotlin using Retrofit library in a simplest way.

Gradle dependency

You need to add below dependencies into your Android projects build.gradle (Module: app) file

API and raw JSON Request Response

Now let’s assume our server endpoint for your REST request is http://api.server.com/users and we want to add a new user in server using http POST method call. Server spec for request and response might look like,

Request body json:

Response body json:

We see, when we hit the endpoint with above request body, server inserts the user info into its database, creates a new user ID for that particular user and returns the same user including the assigned new user ID. We want to parse the response user into the client apps custom object.

Kotlin Data Model Class

We need to prepare model class UserInfo. As we see, json keys for request and response are not following coding convention of Android, so we need to add SerializedName annotation for keys so that when we use Retrofit it generates proper json with those keys. Using SerializedName we can map our property to jason key. Remember it is better to make the properties optional so that even if server does not response with all the keys we still able to create our rich object in client side.

Retrofit Workflow

Now, we need three components to work with Retrofit REST calls,

  1. RestApi interface
  2. ServiceBuilder object
  3. RestApiService class
  1. RestApi interface

In RestApi interface, we declare all the REST request methods with individual paths for the endpoint. Although example is shown for a typical POST call, we can use any REST methods on our own i.e. POST, GET, PUT, DELETE etc.

2. ServiceBuilder object

We create a builder of the retrofit object which can be reused for all method calls declared in the RestApi interface. We can set many properties like, converter factory for json parsing, base url, http client and many more configurations as required. Here is the simplest required form for our tasks.

3. RestApiService class

Finally, we implement the actual Service which we will directly invoke. We call it service but don’t be confused with Android service. If you feel you can rename the class as your own like with RestApiManager or ApiManager etc.

Example of calling

Now we can call RestApiService’s method addUser() from any point of our application. A completion block onResult is used so that the result can be send back to the caller asynchronously. We see if the request succeeds we return the body of the response parsed into our custom object UserInfo to the callers and if the request fails, we return null.

So far this is the simplest workflow to handle a POST request to server and parse the response into custom rich object on Android Kotlin. The same workflow can be used for other REST methods with minimal changes.

--

--