From 0ff4666e20f6c9b079b813f8a2fd7772688f040c Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Wed, 1 Nov 2023 05:01:22 -0400 Subject: [PATCH] content-type always set with body --- packages/components/src/api/MakeRequest.ts | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/packages/components/src/api/MakeRequest.ts b/packages/components/src/api/MakeRequest.ts index b9307a7251..44b534625c 100644 --- a/packages/components/src/api/MakeRequest.ts +++ b/packages/components/src/api/MakeRequest.ts @@ -15,21 +15,30 @@ interface RouteSetup { const formatUrl = (endpoint = "") => `${ useSsl ? "https" : "http" }://${ host + endpoint }`; +const METHOD_OMITS_BODY : { [key: string] : boolean } = { + "DELETE": true, + "GET": true, + "OPTIONS": true, + "PATCH": false, + "POST": false, + "PUT": false, +} + const MakeRequest = (routeSetup: RouteSetup) : (urlRouteArgs: UrlRouteArgs, request: Request) => Promise => { return async function(urlRouteArgs: UrlRouteArgs, request: Request) : Promise { const endpoint = routeSetup.routingFunction(urlRouteArgs); const method = routeSetup.method; - const noBodyMethods = ["GET","HEAD", "DELETE", "OPTIONS"].indexOf(method) > -1; - const isGet = method === "GET"; + + const methodOmitsBody = METHOD_OMITS_BODY[method] || false; return fetch(formatUrl(endpoint), { method, headers: { "Accept": "application/json", - ...isGet ? {} : { "Content-Type": "application/json" } + ...methodOmitsBody? {} : { "Content-Type": "application/json" } }, credentials: 'include', - ...noBodyMethod ? {} : { body: JSON.stringify(request) }, + ...methodOmitsBody ? {} : { body: JSON.stringify(request) }, }) .then(res => res.json()) .then(res => {