0

I'm looking for a way to add 2 custom cookies to every http request.

The browsermob proxy (https://github.com/lightbody/browsermob-proxy) has removeHeaders() and addHeader() methods, but what can I do to keep existing cookies in request, but add 2 more cookies?

Thanks!

  • Could you give more information about how you're using BrowserMob Proxy? Are you running in [Embedded or Standalone mode](https://github.com/lightbody/browsermob-proxy/tree/2.0#embedded-mode)? What language (Java, Python, etc.)? And what version of BrowserMob Proxy? – Jason Hoetger Mar 10 '15 at 18:39
  • The version is 2.1, standalone mode, but I've already found a decision. You could set request handler via REST API. For example curl -X POST -H 'Content-Type: text/plain' -d 'js code here' http://10.100.100.20:8080/proxy/8081/interceptor/request – Dmitrii Voitovich Mar 12 '15 at 13:15
  • Our js code is look like: `$jsHandlerString = sprintf( 'var c = request.getMethod().getFirstHeader("Cookie") ? request.getMethod().getFirstHeader("Cookie").getValue() : ""; request.getMethod().setHeader("Cookie", c + "; %s");', implode('; ', $cookiesArray) );` – Dmitrii Voitovich Mar 12 '15 at 13:16

2 Answers2

0

You can use this method to invoke your custom js code in each request/response https://github.com/lightbody/browsermob-proxy#http-request-manipulation Some example in Python

def response_interceptor(self, js):
"""
Executes the javascript against each response
:param js: the javascript to execute
"""
r = requests.post(url='%s/proxy/%s/interceptor/response' % (self.host, self.port),
          data=js,
          headers={'content-type': 'x-www-form-urlencoded'})
return r.status_code

def request_interceptor(self, js):
"""
Executes the javascript against each request
:param js: the javascript to execute
"""
r = requests.post(url='%s/proxy/%s/interceptor/request' % (self.host, self.port),
          data=js,
          headers={'content-type': 'x-www-form-urlencoded'})
return r.status_code

and test:

def test_request_interceptor_with_parsing_js(self):
"""
/proxy/:port/interceptor/request
"""
js = 'alert("foo")'
status_code = self.client.request_interceptor(js)
assert(status_code == 200)
0

As I answered above, you could use proxy's REST API to set custom js handler on every request, made through the proxy.

For example you could add any custom cookies to every request:

curl -X POST -H 'Content-Type: text/plain' -d 'js code' http://10.100.100.20:8080/proxy/8081/interceptor/request

In php it would look like:

/**
 * @param Proxy $proxyObject
 * @param array $cookiesArray
 */
protected function _setRequestCookies(Proxy $proxyObject, array $cookiesArray)
{
    foreach ($cookiesArray as $nameString => $valueString) {
        $cookiesArray[$nameString] = $nameString . '=' . $valueString;
    }

    $jsHandlerString = sprintf(
        'var c = request.getMethod().getFirstHeader("Cookie") ? request.getMethod().getFirstHeader("Cookie").getValue() : ""; request.getMethod().setHeader("Cookie", c + "; %s");',
        implode('; ', $cookiesArray)
    );

    $urlString = sprintf('%sproxy/%u/interceptor/request', $this->_hubUrlString, $proxyObject->getPort());

    $this->_requesterObject->makeRequest($urlString, Requester::REQUEST_METHOD_POST, $jsHandlerString);
}