Rewrite – Modify the Request and the Response

The Rewrite feature allows you to automatically change parts of HTTP Requests and Responses every time they meet a pre-defined set of rules.


This tutorial will show you how to use the Rewrite feature to change the Request and the Response. Please note that you need to complete the Rewrite – Modify the Request tutorial before starting this one.

We are also going to add a simple Regex expression to the Rewrite Rule to allow us target specific data in the Response body. Combining Rewrite Rules with some simple Regex allows you to create incredibly precise and powerful rules.

Prerequisites

This tutorial assumes you have completed a previous tutorial about Rewrite – Modify the Request

Open your existing Rewrite Rule

Tools -> Rewrite -> double click on your Rewrite Rule to open it

Defining the Rewrite Rule

We want to find every instance in the Response body where the “isTailored” property is set to TRUE and then change it’s value to FALSE. The next step will explain how to do this

Create the Rewrite Rule

Click the “Add” button (the one at the bottom of the screen) to open the Rules screen. Configure as follows:

  • Select “Body” from the “Type” dropdown
  • Untick the “Request” checkbox
  • Tick the “Response” checkbox
  • Paste “isTailored”:\s*false into the “Match” section’s “Value” textfield
  • Also tick the Regex checkbox for the same textfield
  • Paste “isTailored”: true into the “Replace” section’s “Value” textfield
  • Click OK to save the Rewrite Rule
  • Click OK to close the Rewrite Settings screen

Check your Rewrite Rule works

Reload the page in Chrome

Return to Charles. The “isTailored” property should now be set to TRUE

Having problems?

See article about common problems when working with Rewrite rules

One thought on “Rewrite – Modify the Request and the Response

  1. In cases where the URL ends with a slash “/” – be sure to include this in the path component otherwise Charles won’t match it. (Hours lost on this..)

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: