Logo for tanaschita.com

How to intercept and edit a server response with Proxyman for iOS

Learn how to change response data to test different server response scenarios.

18 Sep 2023 · 3 min read

When developing iOS applications, sending and receiving network requests is an essential part of almost every application. During the development or testing process, we often need to test different scenarios on how our app behaves with different server responses.

To be able to do that, we can use Proxyman - which is a proxy server app that enables us to view, inspect and change networking traffic coming in and out from our iOS application.

In this article, we are going to look at how to intercept and change a server response before receiving it on an iOS device or simulator with Proxyman's breakpoint tool. If you are new to Proxyman, check out this guide to get started with Proxyman for iOS.

Let's get started.

Sponsorship logo
Preparing for a technical iOS job interview - updated for iOS 17
Check out my book on preparing for a technical iOS job interview with over 200 questions & answers. Test your knowledge on iOS topics such as Swift, SwiftUI, Combine, HTTP Networking, Authentication, SwiftData & Core Data, Concurrency with async/await, Security, Automated Testing, Machine Learning and more.
LEARN MORE

Let's say, we are developing an iOS application which shows quotes depending on a tag which a user selected. For that, the application sends the following request:

https://api.quotable.io/quotes?tags=technology

With an answer looking as follows:

{
"count": 20,
"totalCount": 50,
"page": 1,
"totalPages": 3,
"lastItemIndex": 20,
"results": [
{
"id": "KMTJ0Ya3e9",
"author": "Thomas Edison",
"content": "To invent, you need a good imagination and a pile of junk.",
"tags": ["Creativity", "Science", "Technology"]
},
{
"id": "sXhSm0WMZqL",
"author": "Alan Watts",
"content": "Technology is destructive only in the hands of people who do not realize that they are one and the same process as the universe.",
"tags": ["Technology"]
},
...

After receiving the response, we show the results in a list.

We also implemented an no results view to show to the user when no quotes are available for a certain tag, so we'd like to test that by changing the response to an empty list.

To change the response to be able to test that scenario, we can follow these steps:

1. Creating a breakpoint

One way to create a breakpoint is to right-click on the request and select tools > breakpoint:

Creating a breakpoint with Proxyman.
Creating a breakpoint with Proxyman.

After doing that, Proxyman opens a settings dialog where we can configure whether we'd like to intercept the request, the response or both and define matching rules for the url to match.

Breakpoint settings.
Breakpoint settings.

In our example, we use the wildcard * for the tags query value to intercept the response for any tag. Since we only want to edit the response, we leave the request option unchecked.

2. Intercepting and modifying the response

After configuring the breakpoint, we can now send a request we'd like to intercept. After doing that, Proxyman will open the following window when receiving a response:

Intercept and change a response.
Intercept and change a response.

In this window, we can edit the response the way we want, for example to an empty results array - as shown above. To continue, we can use the execute button in the bottom right corner.

With this simple setup, we can test many different scenario concerning the server communication. The Breakpoint tool is not only suitable to modify the response's body. We can also use it to modify headers or status code, for example to test different server errors.

Sponsorship logo
Preparing for a technical iOS job interview - updated for iOS 17
Check out my book on preparing for a technical iOS job interview with over 200 questions & answers. Test your knowledge on iOS topics such as Swift, SwiftUI, Combine, HTTP Networking, Authentication, SwiftData & Core Data, Concurrency with async/await, Security, Automated Testing, Machine Learning and more.
LEARN MORE

Newsletter

Image of a reading marmot
Subscribe

Like to support my work?

Say hi

Related tags

Articles with related topics

networking

tools

ios

Quick guide on Proxyman for iOS development

Learn how to use Proxyman to inspect network traffic for your iOS application.

04 Jul 2022 · 8 min read

Latest articles and tips

© 2024 tanaschita.com

Privacy policy

Impressum