artillery icon indicating copy to clipboard operation
artillery copied to clipboard

how to log entire response (at least statusCode & body)

Open cyrfer opened this issue 6 years ago • 7 comments

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.

cyrfer avatar Feb 14 '19 16:02 cyrfer

There's no way to capture the response code at the moment. You have two options that may help:

  1. Run artillery with DEBUG=http,http:response to print the details of every HTTP request made
  2. 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.

hassy avatar Feb 14 '19 16:02 hassy

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"
}

cyrfer avatar Feb 14 '19 17:02 cyrfer

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.

tbradley-sans avatar Sep 20 '19 15:09 tbradley-sans

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
}

JGro144 avatar May 05 '20 00:05 JGro144

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();
} 

cpschult avatar Jul 15 '20 12:07 cpschult

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();
} 

Can you share my-functions.js?

RF2023 avatar Apr 04 '23 21:04 RF2023

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.

eliezercazares avatar Oct 05 '23 20:10 eliezercazares