From f199be4dfc6e986516f22ebd375fc5aece9f69e7 Mon Sep 17 00:00:00 2001 From: Varun0157 Date: Sun, 16 Jun 2024 21:01:22 +0530 Subject: [PATCH] feat: replacing single quotes with '%27' --- src/constructCurl.ts | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/constructCurl.ts b/src/constructCurl.ts index f27691a..ff4798b 100644 --- a/src/constructCurl.ts +++ b/src/constructCurl.ts @@ -3,6 +3,11 @@ import { getStringValueIfDefined } from "./utils/typeUtils"; import { getParamsForUrl, getURL } from "./executeRequest"; import { RequestSpec } from "./models"; +function replaceSingleQuotes(value: T): T { + if (typeof value !== "string") return value; + return value.replace(/'/g, "%27") as T & string; +} + export function getCurlRequest(request: RequestSpec): string { let curl: string = "curl"; @@ -13,13 +18,13 @@ export function getCurlRequest(request: RequestSpec): string { if (request.httpRequest.headers !== undefined) { for (const header in request.httpRequest.headers) { if (header === "user-agent") continue; - curl += ` -H '${header}: ${request.httpRequest.headers[header]}'`; + curl += ` -H '${replaceSingleQuotes(`${header}: ${request.httpRequest.headers[header]}`)}'`; } } // body if (request.httpRequest.body !== undefined) - curl += ` -d '${getStringValueIfDefined(request.httpRequest.body)}'`; + curl += ` -d '${replaceSingleQuotes(getStringValueIfDefined(request.httpRequest.body))}'`; // options.follow if (request.options.follow) curl += " -L"; @@ -31,10 +36,12 @@ export function getCurlRequest(request: RequestSpec): string { if (request.options.showHeaders) curl += " -i"; // url w/ params - curl += ` '${getURL( - request.httpRequest.baseUrl, - request.httpRequest.url, - getParamsForUrl(request.httpRequest.params, request.options.rawParams), + curl += ` '${replaceSingleQuotes( + getURL( + request.httpRequest.baseUrl, + request.httpRequest.url, + getParamsForUrl(request.httpRequest.params, request.options.rawParams), + ), )}'`; return curl;