user10986113
user10986113

Reputation:

Convert request.body to json

I am trying to assert data that I am sending the API.

When I log body I see that it's off ArrayBuffer format. But when I use Buffer.from I get a message First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.

cy.wait(‘@myrequest’)
            .its('request')
            .then((request) => {
                console.log(request.body)

                const buffer = Buffer.from(request.body)
            })

What can I do to retrieve data in form object or json to then assert data that I am sending to an API?

Upvotes: 1

Views: 6667

Answers (2)

user9161752
user9161752

Reputation:

There's an example recipe that has multipart handling here stubs multipart form

Test

cy.wait('@submitForm').its('request').then(({ headers, body }) => {
  expect(body, 'request body').to.be.a('ArrayBuffer')
  const contentType = headers['content-type']

  // the browser sets the separator string when sending the form
  // something like
  // "multipart/form-data; boundary=----WebKitFormBoundaryiJZt6b3aUg8Jybg2"
  // we want to extract it and pass to the utility function
  // to convert the multipart text into an object of values
  expect(contentType, 'boundary').to.match(/^multipart\/form-data; boundary=/)
  const boundary = contentType.split('boundary=')[1]
  const values = parseMultipartForm({ boundary, buffer: body })

  expect(values, 'form values').to.deep.equal({
    city: 'Boston',
    value: '28', // note this is a string
  })
})

Parsing

/*
  Utility: parses (very simply) multipart buffer into string values.

  the decoded buffer string will be something like
  ------We  bKitFormBoundaryYxsB3tlu9eJsoCeY
  Content-Disposition: form-data; name="city"

  Boston
  ------WebKitFormBoundaryYxsB3tlu9eJsoCeY
  Content-Disposition: form-data; name="value"

  28
  ------WebKitFormBoundaryYxsB3tlu9eJsoCeY--

  there are NPM packages for parsing such text into an object:
  - https://www.npmjs.com/package/parse-multipart
  - https://www.npmjs.com/package/multiparty
  - https://www.npmjs.com/package/busboy
  - https://www.npmjs.com/package/formidable
  */
const parseMultipartForm = ({ boundary, buffer }) => {
  expect(boundary, 'boundary').to.be.a('string')
  const decoder = new TextDecoder()
  const decoded = decoder.decode(buffer)

  const parts = decoded.split(`--${boundary}`)
    .map((s) => s.trim())
    .filter((s) => s.startsWith('Content-Disposition: form-data;'))

  console.log(decoded)
  console.log(parts)

  const result = {}

  parts.forEach((part) => {
    const lines = part.split(/\r?\n/g)

    console.log('lines')
    console.log(lines)
    const key = lines[0].match(/name="(.+)"/)[1]

    result[key] = lines[2].trim()
  })

  return result
}

Upvotes: 0

RooterTooter
RooterTooter

Reputation: 412

Assuming that this is javascript, you should be able to use JSON.parse

let jsonData = JSON.parse(request.body);

Note that this should be wrapped in a try {} catch {}

If you're using express, you can try https://www.npmjs.com/package/body-parser

Upvotes: 0

Related Questions