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

Created on 14 Feb 2019  路  5Comments  路  Source: artilleryio/artillery

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.

Most helpful comment

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
}

All 5 comments

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.

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();
} 
Was this page helpful?
0 / 5 - 0 ratings

Related issues

hassy picture hassy  路  3Comments

hartmut-co-uk picture hartmut-co-uk  路  4Comments

lalarsson picture lalarsson  路  3Comments

SamDecrock picture SamDecrock  路  5Comments

damon-kreft picture damon-kreft  路  3Comments