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.
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
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());
});
});
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
.
Alternatively, you can also use the arrival-rate executors in k6 to execute the function at a specific rate per second: Open and closed models