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

138
Vistas
How do I save my request's body as a variable?

I'm trying to save my request's body as a variable on cypress, so I can use the same body for another request. How can I do it?

The code below is working, but I need to save BODY as a variable for another request. Is there a easy way to do it?

cy.request({
    url: "/api/affiliate/create",
    method: "POST",
    headers:
    {
        Authorization: `bearer ${access_token}`,
    },

    body: {

        "address": Random.address,
        "address_complement": Random.address_complement,
        "address_city": Random.address_city,
        "address_number": `${Random.address_number}`,
        "address_district": Random.address_district,
        "address_state": Random.address_state,
        "cep": `${Random.cep}`,
        "cnpj": Random.cnpj,
        "company_name": Random.company_name,
        "headquarters": fAffiliate.headquarters[1],
        "ie": `${Random.ie}`,
        "phone": Random.phone,
        "rntrc": `${Random.rntrc}`,
        "status": fAffiliate.status[0],
        "trading_name": Random.trading_name
    }
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Put it in a variable first, then use the variable in the Cypress call.

const body = {
  "address": Random.address,
  "address_complement": Random.address_complement,
  "address_city": Random.address_city,
  "address_number": `${Random.address_number}`,
  "address_district": Random.address_district,
  "address_state": Random.address_state,
  "cep": `${Random.cep}`,
  "cnpj": Random.cnpj,
  "company_name": Random.company_name,
  "headquarters": fAffiliate.headquarters[1],
  "ie": `${Random.ie}`,
  "phone": Random.phone,
  "rntrc": `${Random.rntrc}`,
  "status": fAffiliate.status[0],
  "trading_name": Random.trading_name
};

cy.request({
  url: "/api/affiliate/create",
  method: "POST",
  headers: {
    Authorization: `bearer ${access_token}`,
  },
  body: body
})

Then you can use the body variable again for a second request.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You may also wish to save it as a fixture, to use in more than one spec.

In /support/index.js

const body = {...}
cy.writeFile('cypress/fixtures/request-body.json', body)

In the test

cy.fixture('request-body.json').then(body => {
  cy.request({
    url: "/api/affiliate/create",
    method: "POST",
    headers: {
      Authorization: `bearer ${access_token}`,
    },
    body
  })
})
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