From c287ae67df61d64a228c604c13b5ab97bd45b926 Mon Sep 17 00:00:00 2001 From: Varun0157 Date: Fri, 12 Jan 2024 10:58:34 +0530 Subject: [PATCH 1/2] feat (curl): added showHeaders option --- src/constructCurl.ts | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/src/constructCurl.ts b/src/constructCurl.ts index c6f6c41..cd086f1 100644 --- a/src/constructCurl.ts +++ b/src/constructCurl.ts @@ -4,33 +4,36 @@ import { getParamsForUrl, getURL } from "./executeRequest"; import { RequestSpec } from "./models"; export function getCurlRequest(request: RequestSpec): string { - const methodFlag = ` -X ${request.httpRequest.method.toUpperCase()}`; + let curl: string = "curl"; - let headersFlag = ""; + // method + curl += ` -X ${request.httpRequest.method.toUpperCase()}`; + + // headers if (request.httpRequest.headers !== undefined) { - for (const header in request.httpRequest.headers) { - headersFlag += ` -H '${header}: ${request.httpRequest.headers[header]}'`; - } + for (const header in request.httpRequest.headers) + curl += ` -H '${header}: ${request.httpRequest.headers[header]}'`; } - let bodyFlag = ""; + // body if (request.httpRequest.body !== undefined) - bodyFlag += ` -d '${getStringValueIfDefined(request.httpRequest.body)}'`; + curl += ` -d '${getStringValueIfDefined(request.httpRequest.body)}'`; + + // options.follow + if (request.options.follow) curl += " -L"; - let followRedirectFlag = ""; - if (request.options.follow) followRedirectFlag = " -L"; + // options.verifySSL + if (!request.options.verifySSL) curl += " -k"; - let verifySSLFlag = ""; - if (!request.options.verifySSL) verifySSLFlag = " -k"; + // options.showHeaders + if (request.options.showHeaders) curl += " -i"; - const url = ` '${getURL( + // url w/ params + curl += ` '${getURL( request.httpRequest.baseUrl, request.httpRequest.url, getParamsForUrl(request.httpRequest.params, request.options.rawParams) )}'`; - const finalCurl = - "curl" + methodFlag + headersFlag + bodyFlag + followRedirectFlag + verifySSLFlag + url; - - return finalCurl; + return curl; } From 70d8bd6599f93c34d032ae5a8668513b75c6cc9d Mon Sep 17 00:00:00 2001 From: Varun0157 Date: Fri, 12 Jan 2024 16:28:06 +0530 Subject: [PATCH 2/2] fix (params): removing loose '?' in empty params showing up in curl --- src/executeRequest.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/executeRequest.ts b/src/executeRequest.ts index f21eeae..78a1863 100644 --- a/src/executeRequest.ts +++ b/src/executeRequest.ts @@ -58,7 +58,7 @@ export async function executeGotRequest(httpRequest: GotRequest): Promise<{ } export function getParamsForUrl(params: Param[] | undefined, rawParams: boolean): string { - if (!params) return ""; + if (!params || params.length < 1) return ""; let paramArray: string[] = []; params.forEach((param) => {