From 199e1c2c208d4450600dc3817b7b7d65149acc79 Mon Sep 17 00:00:00 2001 From: Rene Pfeuffer Date: Wed, 4 Sep 2019 14:47:13 +0200 Subject: [PATCH] Fix content type of requests Without this fix, a content type would be stored in the headers array of the constant fetchOptions once they are set. This way they will be used whenever no explicit content type is set, which is (and mus be) the case for multipart form data. --- .../packages/ui-components/src/apiclient.js | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/scm-ui-components/packages/ui-components/src/apiclient.js b/scm-ui-components/packages/ui-components/src/apiclient.js index 80c2c4dbfa..2ad0cd1f9f 100644 --- a/scm-ui-components/packages/ui-components/src/apiclient.js +++ b/scm-ui-components/packages/ui-components/src/apiclient.js @@ -3,17 +3,16 @@ import { contextPath } from "./urls"; import { createBackendError, ForbiddenError, isBackendError, UnauthorizedError } from "./errors"; import type { BackendErrorContent } from "./errors"; -const fetchOptions: RequestOptions = { - credentials: "same-origin", - headers: { +const applyFetchOptions: (RequestOptions) => RequestOptions = o => { + o.credentials = "same-origin"; + o.headers = { Cache: "no-cache", // identify the request as ajax request "X-Requested-With": "XMLHttpRequest" - } + }; + return o; }; - - function handleFailure(response: Response) { if (!response.ok) { if (isBackendError(response)) { @@ -47,7 +46,7 @@ export function createUrl(url: string) { class ApiClient { get(url: string): Promise { - return fetch(createUrl(url), fetchOptions).then(handleFailure); + return fetch(createUrl(url), applyFetchOptions).then(handleFailure); } post(url: string, payload: any, contentType: string = "application/json") { @@ -73,7 +72,7 @@ class ApiClient { let options: RequestOptions = { method: "HEAD" }; - options = Object.assign(options, fetchOptions); + options = applyFetchOptions(options); return fetch(createUrl(url), options).then(handleFailure); } @@ -81,7 +80,7 @@ class ApiClient { let options: RequestOptions = { method: "DELETE" }; - options = Object.assign(options, fetchOptions); + options = applyFetchOptions(options); return fetch(createUrl(url), options).then(handleFailure); } @@ -99,7 +98,7 @@ class ApiClient { } httpRequestWithBinaryBody(options: RequestOptions, url: string, contentType?: string) { - options = Object.assign(options, fetchOptions); + options = applyFetchOptions(options); if (contentType) { // $FlowFixMe options.headers["Content-Type"] = contentType;