From ba535c24be55e563a95516e7ea83c349f17d77c7 Mon Sep 17 00:00:00 2001 From: bskvorchevsky Date: Tue, 1 Aug 2023 16:57:27 +0300 Subject: [PATCH] fixed application/json post request Set postData format to JSON, when headers contain 'content-type: application/json' --- src/routes.ts | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/routes.ts b/src/routes.ts index b98b3d5..2ecc92d 100644 --- a/src/routes.ts +++ b/src/routes.ts @@ -366,15 +366,21 @@ async function setupPage(ctx: RequestContext, params: BaseRequestAPICall, browse // @ts-ignore overrides[key] = overrideResolvers[key](request) }); - if(overrides?.method === 'POST') { - if(typeof overrides?.postData === 'object'){ - const postData = stringify(overrides?.postData) - overrides = { ...overrides, postData } - } - - if(encode) { - overrides.headers = { ...overrides.headers, 'Content-Type': 'application/x-www-form-urlencoded'} - } + if (overrides?.method === 'POST') { + if (typeof overrides?.postData === 'object') { + const application_json = JSON.stringify(overrides.headers).toLowerCase().includes('application/json') + log.debug('application_json', application_json) + let postData = stringify(overrides?.postData) + if (application_json) + postData = JSON.stringify(overrides?.postData) + log.debug('postData', postData) + overrides = {...overrides, postData} + } + + if (encode) { + overrides.headers = {...overrides.headers, 'Content-Type': 'application/x-www-form-urlencoded'} + } + log.debug('overrides.headers', overrides.headers) } log.debug(overrides)