Convert response body blob to json or plain text in javascript

风流意气都作罢 提交于 2020-04-16 04:14:07

问题


In my cypress test, I have submitted a request and in the response the body returned as blob. How can I check the some text content in body. Is there any way convert the blob into json or plain text. Please see the screenshot attached. Adding the test code below

cy.request('https://someurlHere).then((response) => {
          expect(response.status).to.eq(200) // this is loooking good
          expect(response).to.have.property('headers')  // this is loooking good
          console.log(response.text());
          //var alertArr = [];
          //alertArr = response.json();
          //console.log(alertArr);
        })


回答1:


Just check for response.body. See below example.

cy
  .request('POST', 'http://localhost:8888/users/admin', { name: 'Jane' })
  .then((response) => {
    // response.body is automatically serialized into JSON
    expect(response.body).to.have.property('name', 'Jane') // true
  })


来源:https://stackoverflow.com/questions/57666827/convert-response-body-blob-to-json-or-plain-text-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!