Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

178
Vistas
How to save intercepted request body to outside variable

how can I save request body to outside variable. I want to do something like this:

let request;
  cy.intercept("POST", "/url**", ( req => {
    request = {...req.body};
    req.reply({
      body: response
    });
  }));

// use the "request" variable
// ...

and want to share this copied variable with other functions, but cy.intercept does not allow you to do this. Are there any workarounds?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You will need to add a wait to your code, since cy.intercept() is just a declarative event listener. Adding a wait for it's alias ensures that it has been triggered.

Also, since the code is async you will probably need to wrap and alias request to use it in other parts. Using the raw request variable might give you the empty value, depending on context.

Ideally you would do this in a beforeEach() I think. You probably also need to add the trigger for the POST call - is is a cy.visit()?

let request;

cy.intercept("POST", "**/url/**", ( req => {
  request = {...req.body};
  req.reply({
    body: response
  })
})).as('myIntercept')

// Must cy.wait (not cy.get) the first occurrence
cy.wait('@myIntercept') 

cy.wrap(request).as('myRequest')

// use the "request" variable
cy.get('@myRequest').then(request => {...    

or take request from the wait result

cy.intercept("POST", "**/url/**").as('myIntercept')

// Must cy.wait (not cy.get) the first occurrence
cy.wait('@myIntercept').its('request')
  .then(request => {
    ...
  })

// 2nd time use get()
cy.get('@myIntercept').its('request')
  .then(request => {
    ...
  })
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use aliases for this.

let somevalue;

describe('Test Suite', () => {
  it('Test case', () => {
    cy.intercept("POST", "/url**").as('urlReq')

    cy.get('@urlReq').then((urlReq) => {
      // urlReq.body will have the intercepted request body
      somevalue = urlReq.body.key
    })
  })
})
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda