92

I'm using Charles Proxy to rewrite a response from an API for testing purpose.

If I set a breakpoint, I am allowed to completely rewrite the raw response as I wish.

However, if I want to automate it via the "Rewrite" tool, I'm stuck, it seems that you cannot modify the status code:

Rewrite Rule panel

Do you know if I'm missing something?

Thomas
  • 1,360
  • 1
  • 9
  • 15
  • 1
    I just wondered the same thing. I need exactly this feature right now, so I sent a support request to Charles Proxy's author. I'll report back if I learn of a way to do this. – Marc Liyanage May 09 '13 at 16:50

8 Answers8

143

An Update: Version 3.8 of Charles Proxy was just released, with the ability to rewrite the status. From the release notes:

  • Rewrite tool: allow rewriting of response status

This version's rewrite UI adds a new "Response Status" rule type:

enter image description here

Marc Liyanage
  • 4,601
  • 2
  • 28
  • 28
  • 47
    In my version of Charles (3.10.1) I have to enter both a status code and a message for the rewrite to work. For the example above to work I have to replace "500" with "500 Error". – emidander Apr 22 '15 at 11:14
  • @marc Is it possible to rewrite an API with custom data always until disabled? – Nevin Madhukar K May 20 '21 at 10:47
  • @NevinMadhukarK I'm not sure what exactly you're asking, but if it's about rewriting the response payload, that should be possible as well, but is unrelated to this question. Maybe start a new question thread for that. – Marc Liyanage May 20 '21 at 15:43
39

1- Select Tools > Rewrite or OPTION + CMD + R

Rewrite selection


2- Select Enable Rewrite and then select Add

Enable Rewrite selection


3- Fill the fields with your details

Details


4- Add type and action

Type and Action


5- Select type of action - Response Status for our case -, write value to be changed -500-, write value for replace -200-

Configure Type and Action


Final Step:

6- You are ready to manipulate status code of your response with Charles

Finally

Official site of Charles Web Debugging Proxy

emrcftci
  • 3,355
  • 3
  • 21
  • 35
  • 4
    Best answer here, super helpful with the illustrations, and sticks to the topic: Charles. There's one change needed, though: as of Charles 4.6.2 you need to specify something after the "200" code (in the replace section), e.g. "200 OK" or "200 Banana" - anything, but there must be something. – Roger Oba Feb 03 '22 at 00:16
  • 2
    I'm gonna update the answer, thanks for the comment. – emrcftci Feb 03 '22 at 05:33
19

Using Charles 3.8+, you can rewrite the status code.

In the rewrite tool, select "Type: response status". In the match fields and replace fields, be aware that Charles expects the incoming and rewritten statuses to match the format "\d{3} .*". This means that your rewritten status must have a message portion in addition to the numeric status code.

For example:

Match value: 201 .*

Replace value: 502 Bad Gateway

Omitting the message from the replace value will result in no rewrite of the status line. You can see Charles' rewrite tool output messages in the Notes section of each call's summary.

Community
  • 1
  • 1
Marc Kubischta
  • 193
  • 1
  • 4
11

This is not a direct answer to your question, but as noted in my comment above I was in the same situation and I found a solution using a different tool that works well for me.

Instead of Charles, I run mitmproxy, specifically mitmdump, with this short Python rewriting script:

#!/usr/bin/env python

def response(context, flow):
    if '/somePath' not in flow.request.path:
        return

    flow.response.status_code = 404

To hook it into the proxy, I run mitmdump like this:

mitmdump -s /path/to/rewriting-script.py

and it works great.

I'm on OS X and I configured the network interface's web proxy to 127.0.0.1 port 8080.

Maximilian Hils
  • 6,309
  • 3
  • 27
  • 46
Marc Liyanage
  • 4,601
  • 2
  • 28
  • 28
4

Final Edit: Marc Liyanage's answer is the most correct for this question now.

No, you're not, I don't think it is possible to rewrite a status code.

I cannot add this as a comment (new user) but one workaround is:

  1. Create resources on a server that will return the codes you need, or find ones that do.
  2. Use the Map Remote feature, mapping to the resource that returns the code.

I've tried using Map Local, which would be perfect for this, but Charles adds it's own 200 OK status code to all files returned.

Edit: Also you can use breakpoints on individual responses and modify the code.

David Boydell
  • 1,053
  • 1
  • 8
  • 18
3

Using Requestly Desktop App, not only you can modify the response of a request, but also the status code too and that too within 30 seconds.

On top of this you can:

  • define a regex/wildcard pattern for the URL that needs to be matched.
  • save the rule (so that you don't need to modify the status code again and again).

Here's how a basic response modification rule looks like enter image description here

Here's a quick demo for the same. https://youtu.be/nLcIZGmMAtQ

Sahil Gupta
  • 430
  • 5
  • 12
2

It's not a direct answer to Charles, but you can write JS code to change the HTTP Response at ease with Proxyman (Scripting feature)

For instance,

function onResponse(url, request, response) {
    // Change to 404 status code for the matching request
    response.statusCode = 404; 
    return response;
}

Here is full list of Snippet Code that you can do:

Disclaimer: I'm a creator of Proxyman. Hope it can help you.

Nghia Tran
  • 2,600
  • 2
  • 16
  • 25
0

To enable rewrite for HTTPS you have to enable "SSL Proxy" first in Charles. This article provides the guide to setup this.

SajithK
  • 1,014
  • 12
  • 23