vadersfather
vadersfather

Reputation: 9289

How to use Charles' rewrite tool only on certain methods?

I'm using Charles' Rewrite Tool to change 200 responses to 400 in order to test failing API calls. However, the rewrite is triggering on the Options request. I'd like to only have it trigger on the Get or Post requests and allow the Options requests through. Is this possible using Charles?

Rewrite Screen

Upvotes: 12

Views: 9510

Answers (6)

Per Quested Aronsson
Per Quested Aronsson

Reputation: 12110

A preflight request normally responds with http status code 204 No Content, which makes it easy to separate from the GET response with status code 200 OK. In these cases, you can simply match on the response status in the rewrite rule to replace only the GET response status.

preflight and corresponding get requests

Charles Proxy rewrite rule

Upvotes: 0

Renyi Xie
Renyi Xie

Reputation: 31

In Charles, you can use Breakpoints tools. FYR: https://tanaschita.com/20220307-manipulating-network-requests-and-responses-with-charles/.

Upvotes: 1

Austin
Austin

Reputation: 189

We were able to work around the issue by assuming that OPTIONS would always return an empty body.

The below Regex values will match for GET (because it has a response body) and not match for OPTIONS (because it doesn't have a response body).

\{[\S\s]*\}

or

\[[\S\s]*\]

Rewrite Rule

Upvotes: 8

Noah Tran
Noah Tran

Reputation: 3594

Unfortunately, Charles doesn't have this feature to filter out which the Request that has certain HTTP Method.

It's not a direct answer, but you can achieve with Scripting tool from Proxyman

scripting to change https status code

function onResponse(context, url, request, response) {

  // Update status Code
  response.statusCode = 500;

  // Done
  return response;
}

Here is the Snippet Code that you can do with JS Code.

Disclaimer: I'm a creator of Proxyman. Since there are many people who struggle with this problem, hopefully, the Scripting tool can help you.

Upvotes: 1

Todd Hansberger
Todd Hansberger

Reputation: 487

We have this exact same need to mock API responses. Since the Rewrite tool doesn't support this feature, we have setup Breakpoints on the responses we want to mock, once the breakpoint is hit, we change the response to whatever we want. It works, but is less than ideal.

Upvotes: 6

Lluís Suñol
Lluís Suñol

Reputation: 3651

I think Charles does not have this option, which is really a pitty, because it seems to be easy to implement and it would open the doors to the API world.

I would suggest you to ask Karl (the author and main developer) for this new feature at the contact section of the site.

Upvotes: 6

Related Questions