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

Merged
merged 1 commit into from
Feb 25, 2025

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,

@Murderlon Murderlon merged commit d2db3ad into main Feb 25, 2025
16 checks passed
@Murderlon Murderlon deleted the xhr-response-type branch February 25, 2025 12:48
github-actions bot added a commit that referenced this pull request Feb 25, 2025
| Package          | Version | Package          | Version |
| ---------------- | ------- | ---------------- | ------- |
| @uppy/companion  |   5.5.2 | @uppy/xhr-upload |   4.3.3 |
| @uppy/dashboard  |   4.3.2 | uppy             |  4.13.3 |
| @uppy/status-bar |   4.1.2 |                  |         |

- @uppy/dashboard: do not allow drag&drop of file preview (Merlijn Vos / #5650)
- @uppy/xhr-upload: fix when responseType is set to JSON (Merlijn Vos / #5651)
- meta: build(deps): bump akhileshns/heroku-deploy from 3.13.15 to 3.14.15 (dependabot[bot] / #5659)
- meta: build(deps): bump docker/build-push-action from 6.13.0 to 6.14.0 (dependabot[bot] / #5660)
- @uppy/status-bar: fix aria-hidden warning (Merlijn Vos / #5663)
- @uppy/companion: log when tus uploaded size differs (Mikael Finstad / #5647)
- @uppy/companion: remove redundant HEAD request for file size  (Mikael Finstad / #5648)
- meta: build(deps): bump elliptic from 6.6.0 to 6.6.1 (dependabot[bot] / #5649)
- examples: build(deps-dev): bump esbuild from 0.21.5 to 0.25.0 (dependabot[bot] / #5643)
- meta: build(deps-dev): bump vitest from 1.6.0 to 1.6.1 (dependabot[bot] / #5641)
- meta: build(deps): bump docker/setup-qemu-action from 3.3.0 to 3.4.0 (dependabot[bot] / #5640)
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