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

@uppy/xhr-upload: fix when responseType is set to JSON #5651

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Murderlon
Copy link
Member

Closes #5628

Copy link
Contributor

Diff output files
diff --git a/packages/@uppy/xhr-upload/lib/index.js b/packages/@uppy/xhr-upload/lib/index.js
index 6245c94..b26e493 100644
--- a/packages/@uppy/xhr-upload/lib/index.js
+++ b/packages/@uppy/xhr-upload/lib/index.js
@@ -133,7 +133,7 @@ export default class XHRUpload extends BasePlugin {
     _classPrivateFieldLooseBase(this, _getFetcher)[_getFetcher] = files => {
       return async (url, options) => {
         try {
-          var _this$opts$getRespons, _this$opts2, _body2;
+          var _this$opts$getRespons, _this$opts2, _body3;
           const res = await fetcher(url, {
             ...options,
             onBeforeRequest: (xhr, retryCount) => {
@@ -174,18 +174,23 @@ export default class XHRUpload extends BasePlugin {
           let body = await ((_this$opts$getRespons = (_this$opts2 = this.opts).getResponseData) == null
             ? void 0
             : _this$opts$getRespons.call(_this$opts2, res));
-          try {
+          if (res.responseType === "json") {
             var _body;
-            (_body = body) != null ? _body : body = JSON.parse(res.responseText);
-          } catch (cause) {
-            throw new Error(
-              "@uppy/xhr-upload expects a JSON response (with a `url` property). To parse non-JSON responses, use `getResponseData` to turn your response into JSON.",
-              {
-                cause,
-              },
-            );
+            (_body = body) != null ? _body : body = res.response;
+          } else {
+            try {
+              var _body2;
+              (_body2 = body) != null ? _body2 : body = JSON.parse(res.responseText);
+            } catch (cause) {
+              throw new Error(
+                "@uppy/xhr-upload expects a JSON response (with a `url` property). To parse non-JSON responses, use `getResponseData` to turn your response into JSON.",
+                {
+                  cause,
+                },
+              );
+            }
           }
-          const uploadURL = typeof ((_body2 = body) == null ? void 0 : _body2.url) === "string" ? body.url : undefined;
+          const uploadURL = typeof ((_body3 = body) == null ? void 0 : _body3.url) === "string" ? body.url : undefined;
           for (
             const {
               id,

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

Successfully merging this pull request may close these issues.

XHRUploader does not work with responseType: json
1 participant