From 14e11ebe8bc76276cd12e82400196d6de64ac01d Mon Sep 17 00:00:00 2001 From: Elinor Date: Fri, 8 Apr 2022 12:58:23 +0300 Subject: [PATCH 1/2] validate body option --- src/app/services/actions/snippet-action-creator.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/app/services/actions/snippet-action-creator.ts b/src/app/services/actions/snippet-action-creator.ts index c64196d70..e3fc2d70d 100644 --- a/src/app/services/actions/snippet-action-creator.ts +++ b/src/app/services/actions/snippet-action-creator.ts @@ -30,6 +30,8 @@ export function getSnippetPending(): any { export function getSnippet(language: string): Function { return async (dispatch: Function, getState: Function) => { const { devxApi, sampleQuery } = getState(); + let body: string = ''; + try { let snippetsUrl = `${devxApi.baseUrl}/api/graphexplorersnippets`; @@ -56,13 +58,17 @@ export function getSnippet(language: string): Function { const requestBody = sampleQuery.sampleBody && - Object.keys(sampleQuery.sampleBody).length !== 0 && // check if empty object - sampleQuery.sampleBody.trim() !== '' + Object.keys(sampleQuery.sampleBody).length !== 0 ? JSON.stringify(sampleQuery.sampleBody) : ''; - // eslint-disable-next-line max-len - const body = `${sampleQuery.selectedVerb} /${queryVersion}/${requestUrl + search} HTTP/1.1\r\nHost: graph.microsoft.com\r\nContent-Type: application/json\r\n\r\n${requestBody}`; + if(sampleQuery.selectedVerb === 'GET'){ + // eslint-disable-next-line max-len + body = `${sampleQuery.selectedVerb} /${queryVersion}/${requestUrl + search} HTTP/1.1\r\nHost: graph.microsoft.com\r\nContent-Type: application/json\r\n\r\n`; + } else { + // eslint-disable-next-line max-len + body = `${sampleQuery.selectedVerb} /${queryVersion}/${requestUrl + search} HTTP/1.1\r\nHost: graph.microsoft.com\r\nContent-Type: application/json\r\n\r\n${requestBody}`; + } const options: IRequestOptions = { method, headers, body }; const obj: any = {}; From c442550bdb8c3470d543ed970bc30e61a6b59822 Mon Sep 17 00:00:00 2001 From: Elinor Date: Mon, 11 Apr 2022 22:00:24 +0300 Subject: [PATCH 2/2] use variables --- .../services/actions/snippet-action-creator.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/app/services/actions/snippet-action-creator.ts b/src/app/services/actions/snippet-action-creator.ts index e3fc2d70d..4a36327cf 100644 --- a/src/app/services/actions/snippet-action-creator.ts +++ b/src/app/services/actions/snippet-action-creator.ts @@ -30,7 +30,6 @@ export function getSnippetPending(): any { export function getSnippet(language: string): Function { return async (dispatch: Function, getState: Function) => { const { devxApi, sampleQuery } = getState(); - let body: string = ''; try { let snippetsUrl = `${devxApi.baseUrl}/api/graphexplorersnippets`; @@ -62,12 +61,14 @@ export function getSnippet(language: string): Function { ? JSON.stringify(sampleQuery.sampleBody) : ''; - if(sampleQuery.selectedVerb === 'GET'){ - // eslint-disable-next-line max-len - body = `${sampleQuery.selectedVerb} /${queryVersion}/${requestUrl + search} HTTP/1.1\r\nHost: graph.microsoft.com\r\nContent-Type: application/json\r\n\r\n`; - } else { - // eslint-disable-next-line max-len - body = `${sampleQuery.selectedVerb} /${queryVersion}/${requestUrl + search} HTTP/1.1\r\nHost: graph.microsoft.com\r\nContent-Type: application/json\r\n\r\n${requestBody}`; + const httpVersion = 'HTTP/1.1'; + const host = 'Host: graph.microsoft.com'; + const sampleHeaders = 'Content-Type: application/json'; + + // eslint-disable-next-line max-len + let body = `${sampleQuery.selectedVerb} /${queryVersion}/${requestUrl + search} ${httpVersion}\r\n${host}\r\n${sampleHeaders}\r\n\r\n`; + if(sampleQuery.selectedVerb !== 'GET'){ + body += `${requestBody}`; } const options: IRequestOptions = { method, headers, body };