DEV Community

Cover image for How to Use the Rules Feature in HttpTracer
HttpTracer
HttpTracer

Posted on

How to Use the Rules Feature in HttpTracer

The Rules feature in HttpTracer is one of the most powerful tools for developers who want to intercept and modify network traffic on the fly. Whether you're debugging API responses, testing error handling, or simulating different server behaviors, Rules gives you full control over HTTP requests and responses.

In this guide, we'll walk you through everything you need to know to get started with Rules.


What Are Rules?

Rules allow you to:

  • Match specific network requests based on URL, method, headers, query parameters, and more
  • Modify requests before they're sent to the server (change headers, query params, redirect to a different URL)
  • Modify responses before they reach your app (change status codes, headers, or even replace the entire response body)

This is incredibly useful for:

  • πŸ› Debugging: Test how your app handles different API responses without changing server code
  • πŸ§ͺ Testing edge cases: Simulate 500 errors, slow responses, or unexpected data
  • πŸ”„ Mocking APIs: Return custom responses for endpoints that don't exist yet

Getting Started with Rules

Step 1: Open the Rules Editor

Click on the Rules tab in the sidebar to open the Rules Editor.

You'll see a three-panel interface:

  • Left Panel: Rule Groups and Rules tree
  • Center/Right Panel: Rule configuration (Matchers and Actions)

Step 2: Create a Rule Group

Rules are organized into Groups for better organization. Think of groups as folders for your rules.

  1. Click the "+ Group" button in the left panel
  2. A new group called "New Group" will appear
  3. Double-click the group name to rename it (e.g., "API Debugging Rules")

Step 3: Add a Rule

With your group selected:

  1. Click the "+ Rule" button or right-click the group and select "Add Rule"
  2. A new rule called "New Rule" will be created
  3. Click on the rule to select it and start configuring


Configuring Your Rule

Every rule has two main parts:

  1. Matchers - Define WHICH requests/responses this rule applies to
  2. Actions - Define WHAT to do with matched traffic

Matchers: Targeting Specific Traffic

The Matchers section lets you specify which network requests should be affected by this rule.

Available Request Matchers:

Matcher Description Example
Method HTTP method (GET, POST, etc.) Match only POST requests
Protocol HTTP or HTTPS Match only HTTPS requests
URL Full URL matching Match api.example.com/users
Hostname Just the domain Match api.example.com
Path URL path only Match /api/v1/users
Headers Request headers Match requests with Authorization header
Query Params URL query parameters Match requests with ?debug=true

Matching Conditions:

Each matcher supports different conditions:

  • equals - Exact match
  • contains - Partial match
  • starts-with - Match beginning
  • ends-with - Match ending
  • regex - Regular expression pattern
  • not-equals - Inverse match

Example: To match all GET requests to api.example.com:

  1. Set Method to GET
  2. Set Hostname condition to equals with value api.example.com

Actions: Modifying Traffic

Once you've defined what traffic to match, you can specify what to do with it.

Modify Request Actions:

Enable "Modify Request" to change the request before it's sent:

  • Change Method: Switch GET to POST, etc.
  • Change URL/Hostname/Path: Redirect requests to different servers
  • Add/Remove/Edit Headers: Add authentication, modify content types
  • Add/Remove/Edit Query Params: Inject or remove URL parameters

Modify Response Actions:

Enable "Modify Response" to change the response before your app receives it:

  • Change Status Code: Return 200, 404, 500, etc.
  • Add/Remove/Edit Headers: Modify CORS headers, caching, etc.
  • Override Body: Return custom JSON, HTML, or load from a file


Practical Examples

Example 1: Simulate an API Error

Goal: Test how your app handles a 500 server error from /api/users

  1. Create a new rule
  2. Matchers:
    • Path: contains β†’ /api/users
  3. Actions:
    • Enable "Modify Response"
    • Set Status Code to 500
    • Optionally set body to {"error": "Internal Server Error"}

Now any request to /api/users will return a 500 error!


Example 2: Add Debug Headers

Goal: Add a custom header to all API requests

  1. Create a new rule
  2. Matchers:
    • Hostname: contains β†’ api.
  3. Actions:
    • Enable "Modify Request"
    • Add Header: X-Debug-Mode = true

All requests to domains containing "api." will now include your debug header.


Example 3: Mock an API Response

Goal: Return fake data for an endpoint that doesn't exist yet

  1. Create a new rule
  2. Matchers:
    • Path: equals β†’ /api/v2/new-feature
  3. Actions:
    • Enable "Modify Response"
    • Set Status Code to 200
    • Set Body to:
{
  "feature": "enabled",
  "data": ["item1", "item2", "item3"]
}
Enter fullscreen mode Exit fullscreen mode

Tips & Best Practices

1. Use Rule Groups Effectively

Organize your rules by project, feature, or purpose. You can enable/disable entire groups at once.

2. Toggle Rules On/Off

Each rule has an active toggle. Disable rules you're not currently using without deleting them.

3. Use Regex for Complex Matching

Need to match /users/123 and /users/456? Use regex: /users/\d+

4. Test with Live Traffic

Open the Live panel alongside Rules to see your modifications in real-time.

5. Duplicate Rules

Right-click a rule to duplicate it – great for creating variations of existing rules.


Troubleshooting

Rule Not Working?

  1. Check if the rule is active - Look for the toggle switch
  2. Check if the group is active - Groups can also be disabled
  3. Verify your matchers - Make sure the conditions actually match your traffic
  4. Check the order - Rules are processed in order; earlier rules take precedence

Need More Power?

Consider upgrading your plan to create more rule groups and rules per group.


Summary

The Rules feature transforms HttpTracer from a simple traffic viewer into a powerful debugging and testing tool. By setting up the right matchers and actions, you can:

  • βœ… Test error handling without changing server code
  • βœ… Mock APIs during development
  • βœ… Debug complex request/response flows
  • βœ… Simulate edge cases and rare scenarios

Start with simple rules and gradually explore more advanced features like regex matching, header manipulation, and response body overrides.


Happy debugging with HttpTracer πŸš€

Top comments (0)