Add an HTTP Request Header Using Fiddler CustomRules Magic

Hello World!

In one of my projects, we utilize a custom HTTP header to determine certain properties of the request/user making the request. Now under normal production scenario, the Network Load Balancer (NLB) adds the HTTP header but I had to simulate the behavior without, obviously, having access to an NLB.

As it turns out, Fiddler comes to your rescue! (God Bless Eric Lawrence for this gift to developer-kind). Fiddler has the option to intercept requests matching a pattern (hostname, URL, and the like) and then you can do pretty much anything you like with the request. THIS LINK has a cheat-sheet for the scripts

Let’s see how to add an HTTP header:

  1. Fire up Fiddler (I have version 4.4.9.3, but this option has been available since a long time)
  2. Open up the Rules menu and select customize rules
  3. Fiddler
  4. This will prompt you to download syntax helper (this is optional) – download the tool if you can – I’m behind a corporate firewall so this is not an option for me. Fear not, the syntax of the CustomRules.js is pretty straight-forward and our friend Fiddler keeps a backup copy. If you ever make a mistake, just delete you CustomRules.js and Fiddler will regenerate one for you.
  5. To add a request header, locate the function with this signature:
    static function OnBeforeRequest(oSession: Session)
    
  6. Now, add the following line after the comments
    if (oSession.HostnameIs("localhost")) {oSession.oRequest["X-My-Header"] = "Value";}
    

That’s all! Now any request that is made to the host localhost would have this header added.

NOTE: Remember Fiddler’s capture traffic should be enabled (F12 to toggle enable/disable) for this to work

Happy Coding!

Advertisements
Tagged with: ,
Posted in ASP.NET, Web-Development

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

  • Comic for March 26, 2017
    Dilbert readers - Please visit Dilbert.com to read this feature. Due to changes with our feeds, we are now making this RSS feed a link to Dilbert.com.
%d bloggers like this: