How to get the response and request size and log it

@anilsr I am looking for how to capture the size of the request and response in a policy in Kbs/Mbs, is there any variable available for this purpose.

I would really appreciate your help.

I have seen that Apigee Edge automatically captures this data & send it to analytics. but how can we explicitly log it when the API  transaction happens. is there a flow variable which can be leveraged.?

0 8 2,625
8 REPLIES 8

but how can we explicitly log it when the API transaction happens.

Log it where?

For non-chunked requests and responses, You can read request.header.content-length or response.header.content-length. Does that satisfy?

That gives you the data, but it doesn't log it anywhere. Logging it somewhere would be a separate step.

Thanks, im getting null value for this because my response streaming is on, is there are workaround to get the size with the streaming on.

Apigee  custom reports are getting it not sure how.

Tip: Go thru below documentation which gives all information on how to access on headers.

General information --

One nice way is to download the trace (spend more time in going thru once those trace-<<yyyy>> .xml files ) as you see lot of information & do some fun/good exercise on playing on those variables to understand how to access them..

https://docs.apigee.com/api-platform/reference/variables-reference

To your question you can do js way or assign message way..

sample js way:

var requestMessageSize = context.getVariable("request.header.Content-Length");
var targetMessageSize = context.getVariable("response.header.Content-Length");

more

var clientReceivedContentLength = context.getVariable("client.received.content.length");
var targetReceivedContentLength = context.getVariable("target.received.content.length");

Good Luck.

 

Hi , i tried this. its working only when response.streaming is off,

Is there a way to get the size with response streaming on?

Nope. Not with streaming 'ON.'

Hi, I'm trying to do the same- I might not get the 'content-length' header and I would like to check directly the body size.

As the 'request.content' variable returns a String, I tried using the Blob API to get the bytes size of it (https://www.geeksforgeeks.org/how-to-get-the-length-of-a-string-in-bytes-in-javascript/)- but I'm getting the error: "ReferenceError: \"Blob\" is not defined. "detail":{"errorcode":"steps.javascript.ScriptExecutionFailed"}:

var size = new Blob([request.content]).size;

Could you suggest why the Blob is not working and how to get the size of the body? Thanks

Can you ask a new question please?

ask-a-new-question.png