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.
fileBody
print? Is it[{"OrderId":3588901,"OrderType":"Order","Uuid":"1591821576913"}]
, if so you should readorder[0].OrderId
– Sree.Bh