Modify API response for Android app using Charles proxy

Ilya Eremin
4 min readApr 3, 2017

--

why

For example, your application on start asks your server “hey, budy, am I outdated? What’s the latest version of myself?”, i.e. app sends GET request. Server responds with following:

{
"app_version": 19,
"link": "https://link.to/download/your_app_v19.apk"
}

If user has non latest app version then app downloads latest version and offers user to update. Let’s call this feature ‘autoupdater’.

You are preparing new release. You should make full regression tests on exact apk you are going to upload on Google Play i.e. it points to prod environment. You have to verify autoupdater too.

You can’t change server response because it triggers current production users autoupdater.

how

Proxy. In particular — Charles. Below instruction is for 4.0.2 MacOS version.

prerequiriments

Your phone and your computer should be connected to the same Wi-Fi network.

on your mac

  1. Download, install and launch Charles.
  2. Top bar menu -> Proxy -> Proxy Settings

3. In proxies tab check “Enable transparent HTTP proxying”

4. Top bar -> Proxy -> SSL Proxying Settings… -> SSL Proxying

4.1. Check “Enable SSL Proxying”
4.2. Click Add
4.3. Enter your domain without http/https, for example: your-domain.com
4.4. Ok — OK

5. Cmd+Alt+R to open rewriter tool

5.1. Check “Enable rewriter”
5.2. Click “Add”
5.3. On the right half of the window type request modifier name
5.4. Click first “Add” button. Thus you will add requests filter which you want to rewrite.

5.5. Protocol -> http/https, host -> your-domain.com, Path -> v1/app_info

5.6. Click second “Add”. Thus you point how you want to change request (request itself or response or both).
5.7. Type ->Body, Replace -> Value ->

{
"app_version": 20,
"link": "https://link.to/download/your_app_v20.apk
}

I increased app_version to 20 to trigger autoupdater.

You can rewrite body, headers, status, url — every aspect of the query and its answer.

Before setup Android phone do next:

Connect to Wi-Fi network and press alt and left click on wi-fi networks icon on system panel. Remember “IP Address” section. For me it’s 192.168.1.121

On your Android phone.

  1. Connect to the same Wi-Fi network as your Mac does.
  2. Long press on connected network -> Modify Network. In proxy hostname enter IP Address you previously wrote (192.168.1.121 for me), proxy port is 8888. If you use genimotion then use 10.0.3.2 proxy hostname instead.

3. Open http://www.charlesproxy.com/getssl from your phone to download SSL certificate, downloading begins automatically. Then click on downloaded file to install it. Important, you have to set pin/pattern or any other lock screen security to be able to install SSL certificate.

When using user trusted certificates, Android will force the user of the Android device to implement additional safety measures: the use of a PIN-code, a pattern-lock or a password to unlock the device are mandatory when user-supplied certificates are used.

let’s verify

I tried to modify your-domain.com/v1/app_info endpoint but it does not work. Charles can’t rewrite requests to nonexistent server (try open your-domain.com ;)). So I used different domain. The only requirement is domain should be accessible (if your-domain.com accessible then it does not matter is your-domain.com/v1/app_info endpoint exist or not).

If I open your-domain.com/v1/app_info in browser then I get following:

Charles shows me following:

Request has been successfully modified.

Happy response modifying.

--

--