0

Is it possible to change the har (with json in it) response from an https site? I see the initial json by getText(), then use setText(), and change its size by setSize and setBodySize. The content is changed as I see in debug. But it seems that there is no effect and chrome receives the same old json. Is there a way?

    public static class ResponseModifier implements ResponseInterceptor {

    @Override
    public void process(BrowserMobHttpResponse response, Har har) {

        String contentType = response.getHeader("Content-Type");

        if (contentType!=null && contentType.startsWith("application/json")){
            response.getEntry().getResponse().getContent().setText(respond);
            response.getEntry().getResponse().getContent().setSize(respond.length());
            response.getEntry().getResponse().setBodySize(respond.length());
            response.getRawResponse().removeHeaders("Content-Length");
            response.getRawResponse().addHeader("Content-Length", ""+respond.length());
        }
    }

I think maybe the point is that response.getRawResponse().getEntity().getContentLength() somehow has the old value, but I can't rewrite it. Or is the cause different?

Andrew Regan
  • 4,887
  • 6
  • 35
  • 67
  • You're not rewriting a HAR, but a HTTP response. A HAR is an exported archive of JSON descriptions of requests + responses. Am a bit confused why you would request one via HTTP (and then try to fiddle the response) rather than programatically from the proxy server. – Andrew Regan Feb 12 '16 at 14:48
  • I need to change the http response with json in it, so i thought har is the way. How can I change response content other way? – Andrey Lazuk Feb 12 '16 at 18:43
  • `ResponseInterceptor` is the right way, I think the terminology is confusing though - also the API seems to be in flux. Hang on, this is you? https://github.com/lightbody/browsermob-proxy/issues/382 – Andrew Regan Feb 12 '16 at 20:06
  • Not sure I can help much more. I stopped developing with BMP and was waiting to hear that 2.1 was out and the API situation was fully resolved. You can probably answer this question better yourself now. – Andrew Regan Feb 12 '16 at 20:08

1 Answers1

0

It turn out to be far easier than I thought. I have to use core-littleproxy version of BMP, BrowserMobProxy class and create an instance of BrowserMobProxyServer. The I just use filter:

 proxy.addResponseFilter((response, contents, messageInfo) -> {
        if ((contents.getContentType().startsWith("application/json"))) {
            contents.setTextContents(respond);
        }
    }); 

Thanks for reading :)