Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

97
Views
How to clear request cache after attempting to multiple request in cypress?

Here is my code, I pass array of ids and for each ids I hit Api request.

for (let i = 0; i < 10; i++) {
      cy.request({
        method: 'GET',
        url: `https://[API endpoint ]/api/v1/[requst]/${ids[i]}`,
        headers: {
          "authorization": token
        }
      })
        .then((res) => {
          return usernames.push([{ value: res.body.instagrams[0].username }])
        })
        .then((res) => {
          return cy.wait(2000)
        })
    }

Whenever I execute the code, for 2-3 requests it works fine and then throws error like

Uncaught Error: invalid payload

I can see my URI is proper and sending correct request.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

you should modify your code to wrap all the pending promises into an array. A modified snippet is as below:

    async function processRequest(){
      let result;
      let promises = [];
      for (let i = 0; i < 10; i++) {
        promises.push(cy.request({
                         method: 'GET',
                         url: `https://[API endpoint ]/api/v1/[requst]/${ids[i]}`,
                         headers: {
                            "authorization": token
                        }
            }))
     }

      result = await Promise.all(promises);
      for(let i = 0; i < 10; i++){
        usernames.push([{ value: result[i].username }])
      }
   }
about 4 years ago · Juan Pablo Isaza Report

0

Here cy.request doesn't work properly with Promise. Either while pushing it to promise array, remove cy.request() or purely handle with javascript.

async findUserName(ids, token) {

let promise = []
let usernames = []
ids.forEach(id => {
  promise.push(
    fetch(`https://[host]/api/v1/[endpoint]/${id}`, {
      headers: {
        'Content-Type': 'application/json',
        "authorization": token
      }
    })
      .then((res) => {
        return res.json();
      })
  )
})

let result = await Promise.all(promise);
result.forEach((data) => {
  usernames.push([{ value: data.instagrams[0].username }])
})
return usernames

}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!