Skip to content

Latest commit

 

History

History
47 lines (35 loc) · 1.91 KB

10-request- method- url- -body-- -params- -.md

File metadata and controls

47 lines (35 loc) · 1.91 KB
title description excerpt
request( method, url, [body], [params] )
Issue any type of HTTP request.
Issue any type of HTTP request.
Parameter Type Description
method string Request method (e.g. POST). Note, the method must be uppercase.
url string / HTTP URL Request URL (e.g. http://example.com).
body (optional) string / object / ArrayBuffer Request body; objects will be x-www-form-urlencoded.
params (optional) object Params object containing additional request parameters.

Returns

Type Description
Response HTTP Response object.

Example

Using http.request() to issue a POST request:

import http from 'k6/http';

const url = 'https://httpbin.test.k6.io/post';

export default function () {
  const data = { name: 'Bert' };

  // Using a JSON string as body
  let res = http.request('POST', url, JSON.stringify(data), {
    headers: { 'Content-Type': 'application/json' },
  });
  console.log(res.json().json.name); // Bert

  // Using an object as body, the headers will automatically include
  // 'Content-Type: application/x-www-form-urlencoded'.
  res = http.request('POST', url, data);
  console.log(res.json().form.name); // Bert
}