Charles 進階用法
One can copy each JSON into a text file, save with .charlesrewrite extension,
and import into Charles (Tools → Rewrite → Import).
1. Locale_Header.charlesrewrite
json{ "version": "1.2", "ruleSets": [ { "name": "Locale_Header", "rules": [ { "name": "Add Accept-Language: zh-TW", "enabled": false, "action": "addHeader", "applyTo": "request", "match": {"host": "YOUR-HOST-HERE"}, "parameters": {"header": "Accept-Language", "value": "zh-TW"} } ] } ] }
2. Query_Params.charlesrewrite
json{ "version": "1.2", "ruleSets": [ { "name": "Query_Params", "rules": [ { "name": "Append query consolelog=1", "enabled": false, "action": "addQuery", "applyTo": "request", "match": {"host": "YOUR-HOST-HERE"}, "parameters": {"name": "consolelog", "value": "1"} } ] } ] }
3. Feature_Flags.charlesrewrite
json{ "version": "1.2", "ruleSets": [ { "name": "Feature_Flags", "rules": [ { "name": "Force Feature-Flag header", "enabled": false, "action": "addHeader", "applyTo": "request", "match": {"host": "YOUR-HOST-HERE"}, "parameters": {"header": "X-Feature", "value": "experiment-A"} }, { "name": "Rewrite API version v1 -> v2", "enabled": false, "action": "rewritePath", "applyTo": "request", "match": {"host": "YOUR-HOST-HERE", "path": "/v1/"}, "parameters": {"from": "/v1/", "to": "/v2/"} } ] } ] }
4. Error_Responses.charlesrewrite
json{ "version": "1.2", "ruleSets": [ { "name": "Error_Responses", "rules": [ { "name": "Inject 500 on checkout", "enabled": false, "action": "setStatus", "applyTo": "response", "match": {"host": "YOUR-HOST-HERE", "path": "/v1/checkout"}, "parameters": {"code": 500, "message": "Internal Server Error (simulated)"} }, { "name": "Force 429 on inventory", "enabled": false, "action": "setStatus", "applyTo": "response", "match": {"host": "YOUR-HOST-HERE", "path": "/v1/inventory"}, "parameters": {"code": 429, "message": "Too Many Requests (simulated)"} } ] } ] }
5. Cache_Control.charlesrewrite
json{ "version": "1.2", "ruleSets": [ { "name": "Cache_Control", "rules": [ { "name": "Strip Cache-Control header", "enabled": false, "action": "removeHeader", "applyTo": "response", "match": {"host": "YOUR-HOST-HERE"}, "parameters": {"header": "Cache-Control"} } ] } ] }
6. Geo_Header.charlesrewrite
json{ "version": "1.2", "ruleSets": [ { "name": "Geo_Header", "rules": [ { "name": "Add X-Geo-Country header", "enabled": false, "action": "addHeader", "applyTo": "request", "match": {"host": "YOUR-HOST-HERE"}, "parameters": {"header": "X-Geo-Country", "value": "TW"} } ] } ] }
7. JSON_Field_Patch.charlesrewrite
json{ "version": "1.2", "ruleSets": [ { "name": "JSON_Field_Patch", "rules": [ { "name": "Override JSON field is_new_page -> true", "enabled": false, "action": "replaceBody", "applyTo": "response", "match": {"host": "YOUR-HOST-HERE", "path": "/v1/pages"}, "parameters": { "from": "\"is_new_page\":\\s*false", "to": "\"is_new_page\": true", "isRegex": true } } ] } ] }
8. Multi_Tenant.charlesrewrite
json{ "version": "1.2", "ruleSets": [ { "name": "Multi_Tenant", "rules": [ { "name": "Add X-Tenant QA-TW", "enabled": false, "action": "addHeader", "applyTo": "request", "match": {"host": "YOUR-HOST-HERE", "path": "/v1/"}, "parameters": {"header": "X-Tenant", "value": "qa-tw"} } ] } ] }
📌 Import and Rule enablement
Replace
"YOUR-HOST-HERE"with your real API domain (e.g.,dev-api.project.com).Import each file separately in Charles → Tools → Rewrite.
Keep only the relevant Rule Set enabled per testing scenario.
For production testing — add
⚠prefix to filename to force double-check before enabling.
Comments
Post a Comment