Postman for Android developers

Postman UI

While developing Android applications we often find ourselves working with a back-end, usually through REST APIs. These APIs can be developed by an internal team or by third parties.

To make our work easier while working with APIs we use tools like Retrofit + OkHttp, or Volley. Stetho, a debug bridge for Android, works great with Retrofit and OkHttp. It uses an interceptor which helps analyze the REST API calls, responses, header etc. You can see your API calls using Chrome Developer Tools.

Stetho’s network inspector

Another tool that I find useful is Postman — a desktop app that can execute REST API calls from your computer. It works on Windows, Linux and MacOS.

It’s useful if you want to make sure the response from the API is what you expect, or if you want to use it to create a POJO using jsonschema2pojo.

Sending requests

To send a GET request to the Github issues API we need to:

  1. specify the request type — GET
  2. specify the URL — https://api.github.com/repos/octokit/octokit.net/issues
  3. add the parameters if any — state=closed (We can add the parameters in the Params section or we can write them in the URL field)
  4. add Authorization and/or headers if they are required

After clicking the Send button the request will be executed.

GET request using Postman

At the bottom of the screen we can see the response body (formatted as JSON), the HTTP status code, the time it took for the request to be executed, the response size, headers, etc.

The equivalent Retrofit code is:

@GET("repos/{org}/{repo}/issues")
Call<ResponseBody> getIssuesForRepository(@Path("org") String org, @Path("repo") String repo, @Query("state") String state);

Doing a POST request is similar to a GET request.

  1. From the drop-down menu we select POST as the method
  2. Specify the URL
  3. Set the body

In this example a raw body is used. Other options are ford-data, x-www-form-urlencoded and binary. The JSON (application/json) drop-down adds a Content-Type header.

POST request using Postman

The response is displayed at the bottom just like when doing a GET request.

The equivalent Retrofit code is:

@POST("gists")
Call<ResponseBody> createGist(@Body GistBody body);

To find out more about sending request check out the Postman documentation.

Collections

Requests in Postman can be saved for future use. They can also be organized in Collection.

A request can be saved using the Save button. You can create a Postman account and sign in to have your collections synced across devices.

To find out more about collections check the official documentation.

Variables

I have all my requests saved in a collection and I can execute them in any time. There is one little problem — most requests need an access token that expires after some time and my requests fail with a 401 — Unauthorized status code. Updating all my request when the token changes doesn’t scale well.

To fix this I can define a global variable token and assign it the value of my access token. When the token changes I can update it in a single location.

To define a global variable I click on the eye icon in the top right corner. Then, in my request I can refer to my token as {{token}}. In the example above, the Authorization header will contain the value: Bearer qwert123456.

Environments

When working on an internal app we usually have a few environments like: testing, staging, live, with different API endpoints. Creating an environment variable for the URL enables switching between different endpoints by selecting an item from a drop-down list. This can also be used for the authorization token. The documentation about environments is here.

Environments

Conclusion

Postman is a great tool for working with REST APIs.

You can use collections to save and organize your requests. Variables work great for repeated stuff like the API base URL, authorization tokens, API tokens etc. Environment variables are great for working in different setups like development and production.

Learned something? Click the 💚 to help others find this article.