0
votes

I seem to be having issues interacting with the response body from cy.request. I have two API calls that are both working singularly. I would like to send an id from the response array of the first call to be used by the second call.

Cypress.Commands.add('POST_and_PATCH_Order',() => {
    cy.fixture('orderEntry.json').then((fileBody)=>{

      cy.request({
          method:'POST',
          url:'/api/OrderEntry',
          json: true,
          body: fileBody
      })
    })
    .then(response => {     

        return new Promise(resolve => {
            const target = response.body;
            resolve(target)
        })
    })
})

Then testing with

it("An order may be set to delivered", ()=>{

  cy.POST_and_PATCH_Order().then(order =>{
      const OrderId_val = order['OrderId'];
      cy.request({
        method:'PATCH',
        url:'/api/OrderManager/StatusDelivered',
        json: true,
        body: {
          "OrderId": OrderId_val,
          "ActualPickupDate": "2020-06-08T15:34:20.035Z",
          "ActualDeliveryDate": "2020-06-08T15:34:20.035Z",
          "DeliveryReceiptName": "string"
        }
      })
  })
})

sending OrderId_val as above gets a 404 response with the request body not providing a value for OrderId. Sending order as OrderId results in the entire response header being sent as the OrderId (Body: {"OrderId":[{"OrderId":3588901,"OrderType":"Order","Uuid":"1591821576913"}],"ActualPickupDate":"2020-06-08T15:34:20.035Z","ActualDeliveryDate":"2020-06-08T15:34:20.035Z","DeliveryReceiptName":"string"})

I have tried interacting further with the response.body in command.js & in the test but encounter NullReferenceException outcomes. Would be grateful for any help accessing the OrderId value from the POST response to be used in the subsequent PATCH call.

1
What does fileBody print? Is it [{"OrderId":3588901,"OrderType":"Order","Uuid":"1591821576913"}], if so you should read order[0].OrderIdSree.Bh
I didn't notice there was a comment already. that is exactly what I needed (order[0].OrderId in the test constant that is sent to the second request body). thanks a bunch, I was not finding a ton of examples working with response body that lined up with this scenario.knr
new to Stack Overflow mechanics, but if you push that through as answer I can mark it as a successful answer to my question. thanks again, Sree.Bh!knr
Added as an answer. Happy to help :)Sree.Bh

1 Answers

0
votes

Your fileBody has the data in array format:[{"OrderId":3588901,"OrderType":"Order","Uuid":"1591821576913"}]

You should read it as order[0].OrderId.