From 23f8d31b1b2f5f335174c1f257e36ea8da7ed249 Mon Sep 17 00:00:00 2001 From: Krishna Acondy Date: Fri, 5 Feb 2021 11:10:57 +0000 Subject: [PATCH] fix(*): support multipart form data post --- src/SASViyaApiClient.ts | 17 +++++++++++++++-- src/request/RequestClient.ts | 12 +++++++----- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/SASViyaApiClient.ts b/src/SASViyaApiClient.ts index 6c99376..1a3b95a 100644 --- a/src/SASViyaApiClient.ts +++ b/src/SASViyaApiClient.ts @@ -20,6 +20,7 @@ import { Logger, LogLevel } from '@sasjs/utils/logger' import { isAuthorizeFormRequired } from './auth/isAuthorizeFormRequired' import { RequestClient } from './request/RequestClient' import { NotFoundError } from './types/NotFoundError' +import { SasAuthResponse } from '@sasjs/utils/types' /** * A client for interfacing with the SAS Viya REST API. @@ -693,7 +694,13 @@ export class SASViyaApiClient { } const authResponse = await this.requestClient - .post(url, formData, undefined, 'application/json', headers) + .post( + url, + formData, + undefined, + 'multipart/form-data; boundary=' + (formData as any)._boundary, + headers + ) .then((res) => res.result) return authResponse @@ -733,7 +740,13 @@ export class SASViyaApiClient { } const authResponse = await this.requestClient - .post(url, formData, undefined, 'application/json', headers) + .post( + url, + formData, + undefined, + 'multipart/form-data; boundary=' + (formData as any)._boundary, + headers + ) .then((res) => res.result) return authResponse diff --git a/src/request/RequestClient.ts b/src/request/RequestClient.ts index 39efb4c..37c79b3 100644 --- a/src/request/RequestClient.ts +++ b/src/request/RequestClient.ts @@ -270,14 +270,16 @@ export class RequestClient implements HttpClient { accessToken: string | undefined, contentType: string ) => { - const headers: any = { - 'Content-Type': contentType + const headers: any = {} + + if (contentType !== 'application/x-www-form-urlencoded') { + headers['Content-Type'] = contentType } - if (contentType === 'text/plain') { - headers.Accept = '*/*' - } else { + if (contentType === 'application/json') { headers.Accept = 'application/json' + } else { + headers.Accept = '*/*' } if (accessToken) { headers.Authorization = `Bearer ${accessToken}`