artillery
artillery copied to clipboard
how to log entire response (at least statusCode & body)
I'm trying to correlate the response statusCode with the response body.
How can I get a log of the body & the statusCode? I attempted this:
...blah
scenarios:
- name: POST /foo
flow:
...blah
- post:
url: "/foo"
capture:
- json: "$"
as: fooBody
...blah
- log: "{{statusCode}} => {{ fooBody }}"
Obviously statusCode
logs as undefined
. The fooBody
works.
There's no way to capture
the response code at the moment. You have two options that may help:
- Run
artillery
withDEBUG=http,http:response
to print the details of every HTTP request made - Use a custom
afterResponse
function which will have access to the full response object. That way you can output the information you need in any way you like, save it to a file if needed etc.
Thanks for the response @hassy
Unfortunately using the additional DEBUG settings only added headers.
For example,
Thu, 14 Feb 2019 16:53:35 GMT http:response {
"content-type": "application/json",
"content-length": "32",
"connection": "keep-alive",
"date": "Thu, 14 Feb 2019 16:53:35 GMT",
"x-amzn-requestid": "omitted",
"x-amzn-errortype": "TooManyRequestsException",
"x-amz-apigw-id": "omitted",
"x-cache": "Error from cloudfront",
"via": "1.1 omitted.cloudfront.net (CloudFront)",
"x-amz-cf-id": "omitted=="
}
Thu, 14 Feb 2019 16:53:35 GMT http:response {
"message": "Too Many Requests"
}
Yeah, I discovered earlier this week that DEBUG=http:response
doesn't actually print out the HTTP response status code. That's the single most important part of any response, in my opinion.
If you add processor: "./my-functions.js"
into your test.yml files config then add afterResponse: "printStatus"
to your call in the flow like
- get:
url: "/login"
afterResponse: "printStatus"
then in the my-functions.js file add a
function printStatus (requestParams, response, context, ee, next) {
console.log(`${response.statusCode.uri.path}: ${response.statusCode}`);
return next();
}
make sure in the file you include
module.exports = {
printStatus: printStatus
}
Thanks for the example @JGro144!
I had to make a small modification to get it to work: ( replace statusCode
with request
to retrieve uri path. )
function printStatus (requestParams, response, context, ee, next) {
console.log(`${response.request.uri.path}: ${response.statusCode}`);
return next();
}
Thanks for the example @JGro144!
I had to make a small modification to get it to work: ( replace
statusCode
withrequest
to retrieve uri path. )function printStatus (requestParams, response, context, ee, next) { console.log(`${response.request.uri.path}: ${response.statusCode}`); return next(); }
Can you share my-functions.js?
I used the afterResponse method suggested above.
response.body has the json, you may need to JSON.parse() it in your next method.
and don't quote me but I think you can add to context.vars.yourResponse via YAML and afterResposne method.