0

Please help to get the response body(as json) for an intercepted request using Devtools Network. Below is the code I could attempt. Thanks!

devTools.addListener(Network.requestWillBeSent(),
                entry -> {
                    Request req;
                    RequestId rid=entry.getRequestId();
                    if (entry.getRequest().getUrl().contains("tender")) {
                        req=entry.getRequest();
                    }
                    try {
                        br.write("Request URI : " + entry.getRequest().getUrl()+"\n With method : "+entry.getRequest().getMethod() + "\n");
                    } catch (IOException e) {e.printStackTrace();}

                    Command <ResponseBody> resBody=Network.getResponseBody(rid);

                });
Amresh
  • 315
  • 1
  • 5
  • 14
  • What does your code do? What do you want it to do? What's your actual question? See [ask]. – tgdavies Feb 05 '21 at 10:09
  • My code runs in a selenium framework. I want to capture the response body for some particular network api call. For this I have added a devTools listener that will intercept the request message. I wanted to see the response json for the corresponding request. – Amresh Feb 05 '21 at 10:44
  • Can someone help on this query. Thanks – Amresh Feb 08 '21 at 18:48

1 Answers1

1

Obviously you're just creating the Command-Object, but not executing and retrieving result.

Try this:

Command<GetResponseBodyResponse> getBody = Network.getResponseBody(responseReceived.getRequestId());
GetResponseBodyResponse response = driver.getDevTools().send(getBody);
ObjectMapper objectMapper = new ObjectMapper();
try {
    JsonNode n = objectMapper.readValue(response.getBody(), JsonNode.class);
    debug("Response from Command: " + n.toPrettyString());
} catch (JsonMappingException e) {
        e.printStackTrace();
} catch (JsonProcessingException e) {
        e.printStackTrace();
}

To get this Response Data I registered ResponseReceived-Event using Network.responseReceived() with your DevTools-Instance.