IMLC.ME
/k6/k6 发送 HTTP 请求/

k6 发送 HTTP 请求

HTTP GET

import http from 'k6/http';

export default function () {
  http.get('https://httpbin.org/get');
}

HTTP POST

import http from 'k6/http';

export default function () {
  http.post(
      'https://httpbin.org/post', 
      JSON.stringify({
      "username": "foo",
      "password": "bar"}),
      {
          headers: {
              'Content-Type': 'application/json',
          },
      }
  );
}

HTTP POST

import http from 'k6/http';

export default function () {
  http.del(
      'https://httpbin.org/post', 
      JSON.stringify({
        username: "foo",
        password: "bar"
      }),
      {
          headers: {
              'Content-Type': 'application/json',
          },
      }
  );
}

HTTP DELETE

import http from 'k6/http';

export default function () {
  http.del('https://httpbin.org/post');
}

(容我吐个槽,其他方法都是标准名称,偏偏 HTTP DELETE 用 del 这样的缩写,莫名其妙= =!)

HTTP PATCH

import http from 'k6/http';

export default function () {
  http.patch(
      'https://httpbin.org/patch',
      JSON.stringify({
          name: 'Foo'
      }),
      {
          headers: {
              'Content-Type': 'application/json',
          },
      }
  );
}

HTTP PUT

import http from 'k6/http';

export default function () {
  http.put(
      'https://httpbin.org/put',
      JSON.stringify({
          name: 'Foo'
      }),
      {
          headers: {
              'Content-Type': 'application/json',
          },
      }
  );
}

References

HTTP Requests - k6