requests

The requests module allows issuing HTTP requests in k6 tests. The usual k6 metrics of requests will be generated.

class Response

The Response struct, which contains a server’s response to an HTTP request.

property headers

Dictionary of Response Headers. For example, headers['content-encoding'] will return the value of a 'Content-Encoding' response header.

json(**kwargs)

Returns the json-encoded content of a response, if any.

Parameters:

kwargs – Optional keyword arguments that json.loads takes.

property ok

Returns True if status_code is less than 400, False if not.

This attribute checks if the status code of the response is between 400 and 600 to see if there was a client error or a server error. If the status code is between 200 and 400, this will return True. This is not a check to see if the response code is 200 OK.

property reason

Textual reason of responded HTTP Status, e.g. “Not Found” or “OK”.

property status_code

Integer Code of responded HTTP Status, e.g. 404 or 200.

property text

Content of the response, in unicode.

property url

Final URL location of Response.

delete(url, **kwargs)

Sends a DELETE request.

Parameters:
  • url – URL for the new HTTP request.

  • kwargs – Optional keyword arguments that request takes.

Returns:

Response struct

Return type:

Response

Usage:

load("requests", "delete")

def default(_):
    resp = delete("https://httpbin.org/delete")
get(url, params=None, **kwargs)

Sends a GET request.

Parameters:
  • url – URL for the HTTP request.

  • params – (optional) Dictionary, list of tuples or bytes to send in the query string for the HTTP request.

  • kwargs – Optional keyword arguments that request takes.

Returns:

Response struct

Return type:

Response

Usage:

load("requests", "get")

def default(_):
    resp = get("https://httpbin.org/get")
head(url, **kwargs)

Sends a HEAD request.

Parameters:
  • url – URL for the new HTTP request.

  • kwargs – Optional keyword arguments that request takes.

Returns:

Response struct

Return type:

Response

Usage:

load("requests", "head")

def default(_):
    head = head("https://httpbin.org/head")
options(url, **kwargs)

Sends an OPTIONS request.

Parameters:
  • url – URL for the HTTP request.

  • kwargs – Optional keyword arguments that request takes.

Returns:

Response struct

Return type:

Response

Usage:

load("requests", "options")

def default(_):
    resp = options("https://httpbin.org/options")
patch(url, data=None, **kwargs)

Sends a PATCH request.

Parameters:
  • url – URL for the new HTTP request.

  • data – (optional) Dictionary, list of tuples, bytes, or file-like object to send in the body of the HTTP request.

  • json – (optional) A JSON serializable Python object to send in the body of the HTTP request.

  • kwargs – Optional keyword arguments that request takes.

Returns:

Response struct

Return type:

Response

Usage:

load("requests", "patch")

def default(_):
    resp = patch("https://httpbin.org/patch")
post(url, data=None, json=None, **kwargs)

Sends a POST request.

Parameters:
  • url – URL for the new HTTP request.

  • data – (optional) Dictionary, list of tuples, bytes, or file-like object to send in the body of the HTTP request.

  • json – (optional) A JSON serializable Python object to send in the body of the HTTP request.

  • kwargs – Optional keyword arguments that request takes.

Returns:

Response struct

Return type:

Response

Usage:

load("requests", "post")

def default(_):
    resp = post("https://httpbin.org/post")
put(url, data=None, **kwargs)

Sends a PUT request.

Parameters:
  • url – URL for the new HTTP request.

  • data – (optional) Dictionary, list of tuples, bytes, or file-like object to send in the body of the HTTP request.

  • json – (optional) A JSON serializable Python object to send in the body of the HTTP request.

  • kwargs – Optional keyword arguments that request takes.

Returns:

Response struct

Return type:

Response

Usage:

load("requests", "put")

def default(_):
    resp = put("https://httpbin.org/put")
request(method, url, **kwargs)

Constructs and sends a HTTP request.

Parameters:
  • method – method for the HTTP request: GET, OPTIONS, HEAD, POST, PUT, PATCH, or DELETE.

  • url – URL for the HTTP request.

  • params – (optional) Dictionary, list of tuples or bytes to send in the query string for the HTTP request.

  • data – (optional) Dictionary, list of tuples, bytes or string to send in the body of the HTTP request.

  • json – (optional) A JSON serializable Python object to send in the body of the HTTP request.

  • headers – (optional) Dictionary of HTTP Headers to send with the HTTP request.

  • cookies – (optional) Dict or CookieJar object to send with the HTTP request.

Returns:

Response struct

Return type:

Response

Usage:

load("requests", "request")

def default(_):
    resp = request("GET", "https://httpbin.org/get")