Up and running with BrowserMob Proxy in a 30 seconds and start recording webpage HARs

BrowserMob Proxy is a free (Apache 2.0 license) utility that can capture performance data for web apps (via the HAR format), as well as manipulate browser behavior and traffic, such as whitelisting and blacklisting content, simulating network traffic and latency, and rewriting HTTP requests and responses.

To setup the BrowserMob Proxy, here are the quick steps:
  • Download the BrowserMob Proxy zip file from their website and unzip it. In my case, I have unzipped to /Users/guptam/Documents on a mac machine.
  • Open terminal in the directory in which the unzipped folder resides.
  • Run BrowserMob service at port 8080 (choose different port if required) by running:
      ./browsermob-proxy-2.1.0-beta-6/bin/browsermob-proxy -port 8080
  • Setup the proxy on port 9099 (again, choose different port if required) by running from a different terminal:
      curl -X POST -d 'port=9099' http://localhost:8080/proxy
If everything goes as mentioned above without errors, we should have a proxy running locally at port 9099. Configure your browsers (or any other software) with the proxy.

To record and retrieve HAR (HTTP archive format), here the further steps:
  • Running the following statement would give network calls recorded so far in HAR format and starts a new session.
      curl -X PUT http://localhost:8080/proxy/9099/har
  • Now proceed and complete the activity for which you need network logs.
  • Run the following statement (its same as above one) to retrieve the network logs in HAR format. It would also start a new session.
      curl -X PUT http://localhost:8080/proxy/9099/har
That's it.

Troubleshooting
In case you also get the below error, just run the statements below that fixed the issue in my case.

Error:
  Error: JAVA_HOME is not defined correctly.

Fix:
  export JAVA_HOME=$(/usr/libexec/java_home)
  export PATH=$JAVA_HOME/jre/bin:$PATH