Menu
Open source RSS

Response

Response is used by the http.* methods that generate HTTP request. Those methods return one (or more, in the case of http.batch()) Response objects that contain HTTP response contents and performance timing measurements.

Note that in the case of redirects, all the information in the Response object will pertain to the last request (the one that doesn’t get redirected).

NameTypeDescription
Response.bodystringResponse body content, often used to extract dynamic data (see examples here) and when verifying the presence of content using checks.

See Params.responseType and options.discardResponseBodies for how to discard the body when it is not needed (and to save memory) or when handling bodies with binary data.
Response.cookiesobjectResponse cookies. The object properties are the cookie names and the value is an array of cookie objects (with name, value, domain, path, httpOnly, secure, maxAge and expires fields).
Response.errorstringError message if there was a non-HTTP error while trying to send the request.
Response.error_codenumberError code if there was a non-HTTP error or 4xx or 5xx HTTP error it will be set to a specific code that describes the error. (Added in 0.24.0)
Response.headersobjectKey-value pairs representing all HTTP headers sent by the server. Note that the header names are in canonical form; for example, if the server responds with “accept-encoding”, the key will be “Accept-Encoding”. When requesting a header by a specific name, an array of strings is returned since the header can have multiple values. You can access each value by its index - for the first value, or for single-valued header, that will be Response.headers["my_key"][0].
Response.ocsp.produced_atnumberIf a stapled OSCP response was provided by server, the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC, representing the time when this OCSP stapled response was signed by CA (or by CA entrusted OCSP responder)
Response.ocsp.this_updatenumberIf a stapled OSCP response was provided by server, the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC, representing the time at which the status being indicated was known to be correct.
Response.ocsp.next_updatenumberIf a stapled OSCP response was provided by server, the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC, representing the time when this OCSP stapled response will be refreshed with CA (or by CA entrusted OCSP responder).
Response.ocsp.revocation_reasonstringThe reason for revocation of the certificate (if that is the status), one of the following constants: http.OCSP_REASON_UNSPECIFIED, http.OCSP_REASON_KEY_COMPROMISE, http.OCSP_REASON_CA_COMPROMISE,
http.OCSP_REASON_AFFILIATION_CHANGED,
http.OCSP_REASON_SUPERSEDED,
http.OCSP_REASON_CESSATION_OF_OPERATION,
http.OCSP_REASON_CERTIFICATE_HOLD,
http.OCSP_REASON_REMOVE_FROM_CRL,
http.OCSP_REASON_PRIVILEGE_WITHDRAWN or
http.OCSP_REASON_AA_COMPROMISE.
Response.ocsp.revoked_atnumberIf a stapled OSCP response was provided by server, the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC, representing the time when this certificate was revoked (if that is the status).
Response.ocsp.statusstringThe status of the certificate, one of the following constants: http.OCSP_STATUS_GOOD, http.OCSP_STATUS_REVOKED, http.OCSP_STATUS_UNKNOWN or http.OCSP_STATUS_SERVER_FAILED.
Response.protostringProtocol used to perform the transfer. Possible values are “HTTP/1.0”, “HTTP/1.1”, or “HTTP/2.0”.
Response.remote_ipstringThe IP address of the server handling the request.
Response.remote_portnumberThe port that was connected to on the server side.
Response.request.bodystringRequest body content.
Response.request.cookiesobjectRequest cookies. The object properties are the cookie names and the value is an array of cookie objects (with name, value and replace fields).
Response.request.headersobjectRequest headers.
Response.request.methodstringRequest HTTP method.
Response.request.urlstringRequest URL.
Response.statusnumberHTTP status code returned by the server.
Response.status_textstring(new in k6 v0.29.0) HTTP status text returned by the server.
Response.timingsobjectPerformance timing information for the HTTP request.
Response.timings.blockedfloatContaining time (ms) spent blocked before initiating request.
Response.timings.connectingfloatContaining time (ms) spent setting up TCP connection to host.
Response.timings.tls_handshakingfloatContaining time (ms) spent handshaking TLS session with host.
Response.timings.sendingfloatContaining time (ms) spent sending request.
Response.timings.waitingfloatContaining time (ms) spent waiting for server response.
Response.timings.receivingfloatContaining time (ms) spent receiving response data.
Response.timings.durationfloatTotal time for the request (ms). It’s equal to sending + waiting + receiving, i.e. how long did the remote server take to process the request and respond, without the initial DNS lookup/connection times.
Response.tls_cipher_suitestringIf a TLS session was established, the cipher suite that was used.
Response.tls_versionstringIf a TLS session was established, the version of SSL/TLS that was used.
Response.urlstringThe URL that was ultimately fetched (i.e. after any potential redirects).
Response.clickLink( [params] )functionParses response as HTML, looks for a specific link and does the request-level equivalent of a click on that link.
Response.html()functionReturns an object that supports Selection.find(selector).
Response.json( [selector] )functionParses the response body data as JSON and returns a JS object or array. This call caches the deserialized JSON data, additional calls will return the cached data. An optional selector can be specified to extract a specific part of the data, see here for selector syntax.
Response.submitForm( [params] )functionParses response as HTML, parses the specified form (defaults to looking for a “form” element) with option to override fields and then submits form taking form’s method and action into account.

Example

JavaScript
import { check } from 'k6';
import http from 'k6/http';

export default function () {
  const res = http.get('https://k6.io');
  for (const p in res.headers) {
    if (res.headers.hasOwnProperty(p)) {
      console.log(p + ' : ' + res.headers[p]);
    }
  }
  check(res, {
    'status is 200': (r) => r.status === 200,
    'caption is correct': (r) => r.html('h1').text() == 'Example Domain',
  });
}

A k6 script that will make an HTTP request and print all HTTP response headers