Skip to content

Commit

Permalink
fix: curl export to add data and escape characters
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbrazier committed Jun 23, 2020
1 parent 73b1f5b commit 82931b5
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/RequestDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,23 @@ const RequestDetails: React.FC<Props> = ({ request, onClose }) => {
return JSON.stringify(processedRequest, null, 2);
};

const escapeQuotes = (value: string) => value.replace(/'/g, `\\'`);

const getCurlRequest = () => {
// TODO - currently wont work for every request
let parsedHeaders =
let headersPart =
request.requestHeaders &&
Object.entries(request.requestHeaders)
.map(([key, value]) => `"${key}: ${value}"`)
.map(([key, value]) => `'${key}: ${escapeQuotes(value)}'`)
.join('-H ');
if (parsedHeaders) {
parsedHeaders = `-H ${parsedHeaders}`;
}
headersPart = headersPart ? `-H ${headersPart}` : '';

const body = requestBody && escapeQuotes(requestBody);

const methodPart =
request.method !== 'GET' ? `-X${request.method.toUpperCase()}` : '';
const bodyPart = body ? `-d '${body}'` : '';

return `curl -X${request.method.toUpperCase()} ${parsedHeaders} '${
request.url
}'`;
return `curl ${methodPart} ${headersPart} ${bodyPart} '${request.url}'`;
};

return (
Expand Down

0 comments on commit 82931b5

Please sign in to comment.