Skip to content

Commit

Permalink
fix: backporting some formdata-to-string work into this
Browse files Browse the repository at this point in the history
  • Loading branch information
erunion committed Apr 8, 2024
1 parent aeb4c98 commit f144eb5
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,22 +194,28 @@ export class HTTPSnippet {
const carraige = `${boundary}--`;
const rn = '\r\n';

/*! formdata-polyfill. MIT License. Jimmy Wärting <https://jimmy.warting.se/opensource> */
const escape = (str: string) => str.replace(/\n/g, '%0A').replace(/\r/g, '%0D').replace(/"/g, '%22');
const normalizeLinefeeds = (value: string) => value.replace(/\r?\n|\r/g, '\r\n');

const payload = [`--${boundary}`];
request.postData?.params.forEach((param, i) => {
const name = param.name;
const value = param.value || '';
const filename = param.fileName || null;
const contentType = param.contentType || '';
const contentType = param.contentType || 'application/octet-stream';

if (filename) {
payload.push(`Content-Disposition: form-data; name="${name}"; filename="${filename}"`);
payload.push(
`Content-Disposition: form-data; name="${escape(normalizeLinefeeds(name))}"; filename="${filename}"`,
);
payload.push(`Content-Type: ${contentType}`);
} else {
payload.push(`Content-Disposition: form-data; name="${name}"`);
payload.push(`Content-Disposition: form-data; name="${escape(normalizeLinefeeds(name))}"`);
}

payload.push('');
payload.push(value);
payload.push(normalizeLinefeeds(value));

if (i !== (request.postData.params as Param[]).length - 1) {
payload.push(`--${boundary}`);
Expand Down

0 comments on commit f144eb5

Please sign in to comment.