Capture and Log the Response Body

Capture and log the response body

If I understand you correctly, you want to log the response body? That's a pretty expensive task, but if that's the business requirement...

As @duffymo pointed, a Filter is a suitable place for this. You can capture the response body by replacing the passed-in ServletResponse with a HttpServletResponseWrapper implementation which replaces the HttpServletResponse#getWriter() with an own implementation which copies the response body into some buffer. After continuing the filter chain with the replaced response, just log the copy.

Here's a kickoff example how the doFilter() method can look like:

public void doFilter(ServletRequest request, final ServletResponse response, FilterChain chain) throws IOException, ServletException {
final CopyPrintWriter writer = new CopyPrintWriter(response.getWriter());
chain.doFilter(request, new HttpServletResponseWrapper((HttpServletResponse) response) {
@Override public PrintWriter getWriter() {
return writer;
}
});
logger.log(writer.getCopy());
}

Here's how the CopyPrintWriter can look like:

public class CopyPrintWriter extends PrintWriter {

private StringBuilder copy = new StringBuilder();

public CopyPrintWriter(Writer writer) {
super(writer);
}

@Override
public void write(int c) {
copy.append((char) c); // It is actually a char, not an int.
super.write(c);
}

@Override
public void write(char[] chars, int offset, int length) {
copy.append(chars, offset, length);
super.write(chars, offset, length);
}

@Override
public void write(String string, int offset, int length) {
copy.append(string, offset, length);
super.write(string, offset, length);
}

public String getCopy() {
return copy.toString();
}

}

Map this filter on an url-pattern for which you'd like to log responses for. Keep in mind that binary/static content like images, CSS, JS files and so on won't be logged this way. You'd like to exclude them by using a specific enough url-pattern, e.g. *.jsp or just on the servlet-name of the servlet in question. If you want to log binary/static content anyway (for which I don't see any benefit), then you need to replace the HttpServletResponse#getOutputStream() the same way as well.

How to save requests' response body using Playwright?

I would use waitForResponse that will return the response matching the predicate. The predicate should return true or false. Once the and then you evaluate the response:

const response = await page.waitForResponse(response => response.url().includes('/some_url/') && response.status() === 200);
console.log('RESPONSE ' + (await response.body()));

express logging response body

Not sure if it's the simplest solution, but you can write a middleware to intercept data written to the response. Make sure you disable app.compress().

function logResponseBody(req, res, next) {
var oldWrite = res.write,
oldEnd = res.end;

var chunks = [];

res.write = function (chunk) {
chunks.push(chunk);

return oldWrite.apply(res, arguments);
};

res.end = function (chunk) {
if (chunk)
chunks.push(chunk);

var body = Buffer.concat(chunks).toString('utf8');
console.log(req.path, body);

oldEnd.apply(res, arguments);
};

next();
}

app.use(logResponseBody);

Capturing Response Body for a HTTP Error in python

you can do something like below, which returns the content of the response, in unicode.

response.text

or

try:
r = requests.get('http://www.google.com/nothere')
r.raise_for_status()
except requests.exceptions.HTTPError as err:
print(err)
sys.exit(1)
# 404 Client Error: Not Found for url: http://www.google.com/nothere

here you'll get the full explanation on how to handle the exception. please check out Correct way to try/except using Python requests module?



Related Topics



Leave a reply



Submit