Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
cyrfer opened this issue Feb 14, 2019 · 7 comments
Open

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

cyrfer opened this issue Feb 14, 2019 · 7 comments

Comments

@cyrfer
Copy link

cyrfer commented Feb 14, 2019

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.

@hassy
Copy link
Member

hassy commented Feb 14, 2019

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.

@cyrfer
Copy link
Author

cyrfer commented Feb 14, 2019

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

@tbradley-sans
Copy link

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.

@JGro144
Copy link

JGro144 commented May 5, 2020

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
}

@cpschult
Copy link

cpschult commented Jul 15, 2020

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

@RF2023
Copy link

RF2023 commented Apr 4, 2023

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?

@eliezercazares
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants