AndroidPub
Published in

AndroidPub

Building a proxy server with Ktor

My attempt towards building a proxy server using Ktor as the backbone and some other cool Kotlin libraries.

Most mobile developers and front end developers have been in a situation where they are stuck waiting for the backend services to be ready so that they can test a new flow or screen. QAs also need to quickly test different scenarios which can mostly be produced by different server responses.

This problem has been solved by using HTTP proxies utilities to mock the responses for specific endpoints. I have used a few of them in the past, mitmproxy and Charles to be precise.

A feature image that shows network cables connected to a device.
Photo by Thomas Jensen on Unsplash

Maybe it’s the millennial in me, but I discovered that setting up and using the proxying apps weren’t straightforward. There’s also this step of installing a certificate on my device which leaves an eerie permanent warning on my notification tray.

I stumbled across this video (Building Server Backends with Ktor by Ryan Harter). It got me interested in attempting to build a mocking server which might be easier to use. I was also excited about working on something that isn’t an android project, since I’m mostly and android developer nowadays. After going through the requirements, I came up with this flow:

A very simplified process flow of the mocking server.

The basic setup is to have a server running that will act as the proxy between a request and the original endpoint. My apps will point to this server URL and I’ll always have to set a broker_project_token header value to a token which I can get after creating a project. There is a UI for the server where this can be done. I called the project Broker.

The project is far from finished, it’s a side project. I have used it for most of my recent server responses mocking and it’s been quite good. In general, in other to make use of this proxy server, I’ve had to do these 3 things:

  1. Point my app’s URL to the mocking server’s URL. For example, if my base URL is https://foobar.com then I’ll change that to http://ip_address_where_server_is_running:8080. The ip address has mostly been my computer’s ip address since that’s where I’m running the server.
  2. Add my broker_project_token header value for the project which I intend to mock. On android, it should be similar to this if you’re using OkHttpClient :
An interceptor that adds the header to all my outgoing requests.

3. From Android 9 (API level 28) and above, cleartext support is disabled by default because of the new network security policy. I had to explicitly allow clear text network traffic since I’m running the server on my computer. This stackoverflow answer shows how to do this on android.

In the future, I’ll like to write about the tech stack that I used for this project. I learned a lot while I was working on it. I worked with Ktor, Ktor + Freemarker, kotlin coroutines, Koin, Gradle Kotlin DSL, Potassium Nitrite and some new features in Kotlin.

The (retired) Pub(lication) for Android & Tech, focused on Development

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ben Daniel A.

Ben Daniel A.

Software developer. Learning everyday.

More from Medium

Meet Mockative – A Framework For Mocking Dependencies in Kotlin

Adding a new target to your KMP project: hello watchOS

Publish Your First Package With Gradle and GitLab

Kotlin, Coroutines and, Ktor