Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(formdata): fixed automatic addition of the Content-Type header …
…for FormData in non-browser environments; (#5917)
  • Loading branch information
DigitalBrainJS committed Sep 24, 2023
1 parent 4c89f25 commit bc9af51
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/adapters/xhr.js
Expand Up @@ -61,11 +61,16 @@ export default isXHRAdapterSupported && function (config) {
}
}

let contentType;

if (utils.isFormData(requestData)) {
if (platform.isStandardBrowserEnv || platform.isStandardBrowserWebWorkerEnv) {
requestHeaders.setContentType(false); // Let the browser set it
} else {
requestHeaders.setContentType('multipart/form-data;', false); // mobile/desktop app frameworks
} else if(!requestHeaders.getContentType(/^\s*multipart\/form-data/)){
requestHeaders.setContentType('multipart/form-data'); // mobile/desktop app frameworks
} else if(utils.isString(contentType = requestHeaders.getContentType())){

This comment has been minimized.

Copy link
@Romick2005

Romick2005 Sep 27, 2023

Why do you create contentType variable? Having assigment in if and declaring variable outside the desired scope is a bad practice. Don't you think so @DigitalBrainJS?
Isn't it better to have smth like this:

else {
  const contentType = requestHeaders.getContentType();
  
  if (utils.isString(contentType)) {
    // fix semicolon duplication issue for ReactNative FormData implementation
    requestHeaders.setContentType(contentType.replace(/^\s*(multipart\/form-data);+/, '$1'))
  }
}
// fix semicolon duplication issue for ReactNative FormData implementation
requestHeaders.setContentType(contentType.replace(/^\s*(multipart\/form-data);+/, '$1'))
}
}

Expand Down

0 comments on commit bc9af51

Please sign in to comment.