Rewrite – Modify the Response

The Rewrite feature allows you to automatically modify distinct parts of the Response. Here are some helpful use cases:

  • Quickly generate 4XX/5XX error scenarios that would otherwise be difficult or time consuming to create
  • Continue to do front-end testing even when services throw 4XX/5XX errors
  • Save Responses that are hard to recreate so they can be re-used later

The Rewrite feature has a long list of other rules you can add to your Response. You can add rules to:

  • add, edit or delete headers
  • change the status code
  • change the URL or path
  • change a parameter/query string
  • change the entire Response body
  • change parts of the Response body

This tutorial will show you how to change the Response body and status code to mimic a 500 error from the server.

Prerequisites

Go to the OpenBank API Products’ endpoint

Open Chrome and go to NAB’s Products endpoint

You should now get an error about about a “Missing mandatory header” (the missing header is called ‘x-v’)

Enable SSL Proxying

In Charles, right click the OpenBank request and click the “Enable SSL Proxying” option

The Request(s) you want to modify must be decrypted before you can use the Rewrite feature with it

Reload the page on Chrome

Return to Charles where you should now get a 406 error. Request details should be displayed in plain text

Copy URL

Right click the Request and click the “Copy URL” option

Defining the Rewrite Rule

Let’s create a Rewrite Rule with 2 parts. The first will change the status code from 406 to 500. And the second will overwrite the Response body with a totally new JSON payload

Open Rewrite screen

Tools -> Rewrite -> tick the “Enable Rewrite” checkbox

Configure the Rewrite Location

Add -> Click Add (from the ‘Location’ section) -> Paste URL into the “Host” field -> press the “tab” on your keyboard (this auto populates most of the fields) -> set the “Port” to 443

This is a good article which explains what port 443 is all about

Create rule to change Response status code

Click Add (from the ‘Rule’ section) and configure as follows:

  • Select “Response Status” from the “Type” dropdown
  • Enter 406 into the “Value” textfield of the “Match” section
  • Enter 500 into the “Value” textfield of the “Match” section
  • Click OK to save the rule
  • Click OK to close the Rewrite Settings screen

Check status code rule works

Click the “Repeat” button to resend the network Request

You should now see the status code change to 500

Create rule to change Response body

Click Add (from the ‘Rule’ section) and configure as follows:

  • Select “Body” from the “Type” dropdown
  • Enter the below JSON payload into the “Value” textfield of the “Replace” section
  • Then click OK to save the rule
  • Click OK to close the Rewrite Settings screen

{
  ”code”: “CHARLES_DOCS_Y_ERROR”,

  ”detail”: “Something, somewhere wrong for some unknown reason!”
}

Check Response body rule works

Click the “Repeat” button to resend the Request

You should now see a the new Response body

Having problems?

See article about common problems when working with Rewrite rules

Leave a comment