Skip to content

Commit

Permalink
feat: replacing single quotes with '%27'
Browse files Browse the repository at this point in the history
  • Loading branch information
Varun0157 committed Jun 16, 2024
1 parent cf753fe commit f199be4
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/constructCurl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import { getStringValueIfDefined } from "./utils/typeUtils";
import { getParamsForUrl, getURL } from "./executeRequest";
import { RequestSpec } from "./models";

function replaceSingleQuotes<T>(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";

Expand All @@ -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";
Expand All @@ -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;
Expand Down

0 comments on commit f199be4

Please sign in to comment.