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

Incompatible with Service Workers and FormData #20

Open
conor-odro opened this issue Oct 4, 2022 · 2 comments
Open

Incompatible with Service Workers and FormData #20

conor-odro opened this issue Oct 4, 2022 · 2 comments

Comments

@conor-odro
Copy link

I recently came across an issue that is somewhat related to #16 where I was trying to upload a file to an AWS S3 bucket with a Presigned POST URL from inside a Manifest V3 Chrome Extension which requires the use of Service Workers.

axios.defaults.adapter = fetchAdapter;

const formData = new FormData();
Object.entries(fields).forEach(([key, value]) => {
  formData.append(key, value);
});
formData.append('file', file);
await axios.post(s3Url, formData);

Unfortunately this was erroring due to the Content-Type header being set to application/www-x-form-urlencoded instead of multipart/form-data and the WebKit boundary injected by the browser.

Looking into the code it seems the issue is caused by the use of isStandardBrowserEnv() inside the check which should automatically remove the Content-Type header, allowing the browser to fill in the correct one. This helper method is expecting window and document to be defined which is not possible in a Service Worker environment.

if (method !== 'GET' && method !== 'HEAD') {
    options.body = config.data;

    // In these cases the browser will automatically set the correct Content-Type,
    // but only if that header hasn't been set yet. So that's why we're deleting it.
    if (isFormData(options.body) && isStandardBrowserEnv()) {
        headers.delete('Content-Type');
    }
}

@vespaiach Removing the isStandardBrowserEnv() from the if statement solves my problem and allows the file upload to succeed, but I am unsure if it will have any knock on effect to other use cases of this package?

@rkovalov
Copy link

rkovalov commented Oct 6, 2022

faced with the same issue

@jimmywarting
Copy link

It's way better to check if it's a spec compliant FormData

You could use options?.body?.[Symbol.toStringTag] === 'FormData' to check if it should or shouldn't delete the header.
instead of using if (isFormData(options.body) && isStandardBrowserEnv())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants