Calling API based on the returned response

So I am trying a challenge that I got a month ago:
http://challenge.com/challenges?nickname=anythinghere
http://challenge.com/challenges?nickname=anythinghere
I first need to make a post request at this endpoint in order to receive an ID this is the res structure
id: 'fSkslN++h0BGxtMwlS/OB+OZW+NaIfLqP7dpM0+Zj+WNipwKJMSJJO2wAq4JI5Zs',
actives_at: 1700583237548,
called_at: 1700583236759,
total_diff: 0
id: 'fSkslN++h0BGxtMwlS/OB+OZW+NaIfLqP7dpM0+Zj+WNipwKJMSJJO2wAq4JI5Zs',
actives_at: 1700583237548,
called_at: 1700583236759,
total_diff: 0
2) I then need to use the ID by passing it inside the header as "X-Challenge-Id": {passing the id I got as a response here} on this endpoint
http://challenge.com/challenges?nickname=anythinghere
http://challenge.com/challenges?nickname=anythinghere
the method is set to PUT this time and send a request to this endpoint at the scheduled invocation time. 3)Then I need to make the calls again and again till total_diff here reaches more than 500ms 4) upon performing this all correctly the res given is changed to something like this
{
"result": {
"attempts": 100,
"url": "/xxxxxxxx"
}
}

{
"result": {
"attempts": 100,
"url": "/xxxxxxxx"
}
}

now I am confused since in my case after I pass in the id as a header and then call the endpoint it is only able to make the call 2 or 1 times which results in the challenge failing and the URL being returned as undefined however, My friend was able to make then 2000 calls? can someone help?
1 Reply
Manshul
Manshul10mo ago
async function intervalCall() {
try {
const startResponse = await fetch(
"http://example/challenges?nickname=Bozo",
{ method: "POST" }
); //make the initial fetch call to start the challenge
const dataRes = await startResponse.json();
console.log(dataRes);

// {
// id: 'unique ID',
// actives_at: 1700570652300,
// called_at: 1700570651628,
// total_diff: 0
// }

let dataResId = dataRes.id;
let currentInterval = dataRes.total_diff;

if (currentInterval <= 500) {
const startResponsePut = await fetch(
"http://example/challenges?nickname=Bozo",
{ method: "PUT", headers: { "X-Challenge-Id": dataResId } }
); //make the PUT call with the challenge id untill the differrence is >500
const responsePutJson = await startResponsePut.json();
console.log(responsePutJson);
currentInterval += dataRes.total_diff;
} else {
console.log("Challenge interval exceeds 500");
}
} catch (error) {
console.log("Error", error);
}
}

// Call the function
intervalCall();
async function intervalCall() {
try {
const startResponse = await fetch(
"http://example/challenges?nickname=Bozo",
{ method: "POST" }
); //make the initial fetch call to start the challenge
const dataRes = await startResponse.json();
console.log(dataRes);

// {
// id: 'unique ID',
// actives_at: 1700570652300,
// called_at: 1700570651628,
// total_diff: 0
// }

let dataResId = dataRes.id;
let currentInterval = dataRes.total_diff;

if (currentInterval <= 500) {
const startResponsePut = await fetch(
"http://example/challenges?nickname=Bozo",
{ method: "PUT", headers: { "X-Challenge-Id": dataResId } }
); //make the PUT call with the challenge id untill the differrence is >500
const responsePutJson = await startResponsePut.json();
console.log(responsePutJson);
currentInterval += dataRes.total_diff;
} else {
console.log("Challenge interval exceeds 500");
}
} catch (error) {
console.log("Error", error);
}
}

// Call the function
intervalCall();
Want results from more Discord servers?
Add your server