Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multipart boundary mismatch when using search params #209

Closed
alanshaw opened this issue Nov 26, 2019 · 2 comments · Fixed by #211
Closed

Multipart boundary mismatch when using search params #209

alanshaw opened this issue Nov 26, 2019 · 2 comments · Fixed by #211
Labels
bug Something isn't working

Comments

@alanshaw
Copy link

alanshaw commented Nov 26, 2019

Multipart boundary in content-type does not match boundary in body when using ky with searchParams. This bug was introduced in #208

The following example is effectively what happens here

ky/index.js

Line 242 in d6ff489

this.request = new globals.Request(new globals.Request(url, this.request), this._options);

const fd = new FormData
fd.append('test', new Blob(['data'], { type: 'application/octet-stream' }))

const opts =  { method: 'post', body: fd }
const r0 = new Request('/test', opts)
const r1 = new Request('/test?foo=bar', r0)
const r2 = new Request(r1, opts)

console.log(r2.headers.get('content-type'))
// multipart/form-data; boundary=---------------------------5618055171927212007156353948

await r2.text()
/*
"-----------------------------14683037551046622608547620079
Content-Disposition: form-data; name=\"test\"; filename=\"blob\"
Content-Type: application/octet-stream

data
-----------------------------14683037551046622608547620079--
"
*/
@sholladay
Copy link
Collaborator

sholladay commented Nov 28, 2019

Seems like Request really needs a setter for the URL or some way to modify the URL when cloning a Request. Doing so is far too difficult to do correctly at the moment. If we had that, we wouldn't need to compose requests to support searchParams and this bug could be completely avoided.

We should be able to workaround the bug by deleting the content type header first, as proposed in one of the open PRs, but that isn't a great solution. I wonder if we can reconfigure the request.url property to be writable... Still hacky, but better, IMO.

@sindresorhus sindresorhus added the bug Something isn't working label Nov 29, 2019
@gydroperit
Copy link
Contributor

gydroperit commented Dec 2, 2019

Current workaround - just delete header in beforeRequest hooks( should add at the end)

  beforeRequest: [
      ...,
      (request,_options) => {
        if(_options.body instanceof FormData){
          request.headers.delete('content-type');
          return new Request(request, _options)
        }
        return request;
      }
    ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants