chai-http icon indicating copy to clipboard operation
chai-http copied to clipboard

Status 400 when i try to attach multipart pdf files type form data

Open Krzeminska opened this issue 5 years ago • 4 comments

I'm trying to ask in this way:

request.post('/properUrl')
                .set('Content-Type', 'multipart/form-data')
                .set('Accept', 'application/json')
                .attach('file', './files/test.pdf', 'test.pdf')
                .type('formData')
                .then((response) => {
                    expect(response).to.have.status(200);
                })

I tried also with sending type of data in field: .field('type', 'formData') and the server say's, it's not proper way (responses with 400). Curl of proper request is:

curl -X POST --header 'Content-Type: multipart/form-data' --header 'Accept: application/json'  {"type":"formData"} '/properUrl'

Anyone knows what happen?

Krzeminska avatar Dec 11 '19 15:12 Krzeminska

Hey @Krzeminska, thanks for the issue! I'm not sure off the top of my head, but I would suggest looking at the superagent API to see if there's an answer there? If you are still having trouble, let me know and I'll see if I can reproduce. Do you control the other server/ can you see what the incoming request looks like?

austince avatar Dec 11 '19 15:12 austince

Yes, i was on this page, and tried to fix it with using its examples. Doesn't work with my issue. On server side i see nothing, because it could not parse header, so do not enter to the method (So i cannot debug it).

Krzeminska avatar Dec 11 '19 15:12 Krzeminska

Ok, no worries. Do any of these issues help?

  • https://github.com/chaijs/chai-http/issues/111
  • https://github.com/chaijs/chai-http/issues/168
  • https://github.com/chaijs/chai-http/issues/174

austince avatar Dec 11 '19 15:12 austince

Yes, I tried, as this issues mention, using:

  • .attach('file', fs.readFileSync(file_path))
  • .field()

Krzeminska avatar Dec 11 '19 15:12 Krzeminska