Inspecting HTTP Traffic With Instruments

Xcode 13 added a useful network instrument to record and analyze URL sessions and HTTP traffic.

Recording A Network Trace

To record a network trace, launch the profiler from the Xcode product menu (⌘I) and select the Network template:

Network profile template

There are some limitations. It requires Xcode 13 and an iOS 15 target. It also only works on a device. You cannot record a trace on the simulator. Start a recording (⌘R) and generate some network traffic:

Instruments Record button

I don’t find the Instruments user interface easy to use. By default, the top track shows the HTTP traffic and the lower track shows network connections (bytes transmitted and received):

HTTP Traffic and Network Connections tracks

The lower detail pane defaults to a summary of the session task durations:

Summary Task Durations

You can switch the detail pane to list URLSession Tasks or HTTP Connections. When listing tasks, the extended detail pane shows a code backtrace:

List URLSession tasks

When listing HTTP transactions the extended detail pane shows the request and response headers and the response body:

List HTTP Transactions

If you expand the HTTP Traffic track, it defaults to showing you URLSession tasks organised by process, session name and then domain name. Zoom in to see the task request details:

HTTP Traffic Track by session

If you click on the tiny control below any of the track titles you can switch between showing session tasks and HTTP transactions:

Track Display by Tasks or HTTP Transactions

To inspect an individual track, hover the mouse over the track and click the small + button:

Add to detail button

The track is added to the detail pane where you can then inspect the request and response. In this example my request is failing with an invalid user name:

Detailed inspection of HTTP connection

If you’ve used third-party tools like Charles proxy you might be disappointed. You cannot modify any of the headers and viewing the headers and body is limited to what you see in the extended detail pane:

Body response detail

Name Your Sessions And Tasks

One big tip. Make sure you name your URLSession and tasks:

// Default Session description
defaultSession.sessionDescription = "WorldStore"

// Task to fetch country data
task.taskDescription = "CountryFetchRequest"

This makes it much easier to find and filter for specific task requests.

Sharing A Network Trace

If you need to share the trace outside of Xcode convert it to the HTTP Archive Format (HAR). Save the trace from Instruments and then use xctrace to convert the trace file to the har format:

$ xcrun xctrace export --input Facts.trace --har

The resulting .har file is a JSON file containing the HTTP connection trace information.

Learn More