Rewrite rules – Common Problems

Rewrite rules are an awesome tool but there are a few gotcha’s you should be aware of when using them. This post might save your future self hours of futile debugging!

Unescaped characters

“$” symbol in Response body

  • If you have an unescaped “$” in your Response body value, the response can sometimes fail to complete
  • See full detail of the problem this Stack Overflow post

Escaping a character

  • Simply put a “\” character in front of the “$” symbol

Using multiple Rewrite rules

Rules are independent

  • Unfortunately Rewrite Rules are totally independent of each other
  • For example, assume you are trying to generate a 404 error response (status code and body together). See screenshot
  • If, for example, the status code actually comes back as 403, the Body value will still be changed
  • This problem is common when working with the OPTIONS HTTP methods

Rewrite Rules and the OPTIONS method

Dealing with OPTIONS

  • Using Rewrite rules on endpoints with an OPTIONS HTTP method can be especially tricky
  • As mentioned previously, Rewrite rules are independent of each other
  • See more detail the problem on Stack Overflow post

The workaround for GET and POST

  • If you need to modify the Response body of the related POST or GET request you can add a rule to only match on response with bodies i.e. a OPTIONS response doesn’t contain a body
  • Add either \{[\S\s]*\} or \[[\S\s]*\] into the “Value” textfield of the “Match” section

Leave a Reply

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

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

Google photo

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

Twitter picture

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

Facebook photo

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

Connecting to %s

%d bloggers like this: