How to add callback or promise for Http.post calls

How to add a callback or add a promise for Http.post call in k6
http.post(url, body, params)

I need to call http.get call after the http.post . Based upon the post response I have to call get call.

Requesting you to help me out for the same.

Hello and welcome!

k6 calls HTTP requests synchronously, meaning execution will halt until a response is received, so there is no need for callbacks or promises. All you need to do is return the Response from the http.post and query it for what it is you’re after. The exact code would depend on the nature of the response contents and what it is you’re looking for.

Agreed , but somehow it is not waiting for the response and moving on to the next call.

I implemented the socket mechanism , calling both the http.get calls after some time intervals .

Still it is not waiting for the response.

Can you share some code, please?

const url = “ws://echo.websocket.org”;
let responses = null

  1. ws.connect(url, null, function (socket) {
    socket.on(‘open’, function () {
    console.log(‘WebSocket connection established!’);
    socket.setInterval(function timeout() {
    responses = http.get(pathUrl, params);
    console.log('Responses : ’ + JSON.stringify(responses.body));
    check(responses, {
    ‘responses url status 200’: (responses) => responses.status === 200
    });
    socket.close();
    }, 3000);
    });

    socket.on(‘close’, function () {
    console.log(‘WebSocket connection disconnected!’);
    });

    socket.on(‘error’, function (e) {
    console.log('An unexpected error occured: ', e.error());
    });

});
  1. ws.connect(url, null, function (socket) {
    socket.on(‘open’, function () {
    console.log(‘WebSocket connection established!’);
    socket.setInterval(function timeout() {
    let responses1 = http.get(pathUrl + responses, params);
    console.log('Responses1 : ’ + JSON.stringify(responses1.body));
    check(responses1, {
    ‘responses1 url status 200’: (responses1) => responses1.status === 200
    });
    socket.close();
    }, 3000);
    });

    socket.on(‘close’, function () {
    console.log(‘WebSocket connection disconnected!’);
    });

    socket.on(‘error’, function (e) {
    console.log('An unexpected error occured: ', e.error());
    });

    });

My second call is getting time out . Requesting you to let us know , how to increase the default timeout of a api call in k6

Unless you are specifically testing a WebSocket implementation, you should definitely not be using it just so you can use setInterval - that’s not its intended purpose. If you need to perform something on an interval, that’s what iterations are for.

Just start with the http.post, do what you need with the response, and then add the conditional for the subsequent http.get. Using the example POST from the docs:

import http from 'k6/http';

export default function () {
  var url = 'http://test.k6.io/login';
  var payload = JSON.stringify({
    email: 'aaa',
    password: 'bbb',
  });
  var params = {
    headers: {
      'Content-Type': 'application/json',
    },
  };

  const res = http.post(url, payload, params);

  // call http.get if the status was 200:
  if (res.status === 200) {
    http.get("http://some.other.url.com");
  } else {
    console.log("response status code: " + res.status);
  }
}

To run the function repeatedly on an interval, you would specify a duration or a set number of iterations when you execute k6 run. You might also want to include a sleep statement at the end of the function as otherwise the function will execute as soon as it has completed. The sleep would take a value of 3 (it is expressed in seconds) if you wanted it to be equivalent of what you’re using in your setInterval.

1 Like

Alternatively, you can also use the arrival-rate executors in k6 to execute the function at a specific rate per second: https://k6.io/docs/using-k6/scenarios/arrival-rate/