Skip to content

Commit

Permalink
Handle urlencoded request body
Browse files Browse the repository at this point in the history
Fixes #70
  • Loading branch information
luisfpg committed Feb 19, 2020
1 parent 0d05410 commit 56c469f
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion templates/requestBuilder.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,23 @@ export class {{ requestBuilderClass }} {
} else {
this._bodyContentType = contentType;
}
if ((this._bodyContentType || '').startsWith('multipart/form-data')) {
if (this._bodyContentType === 'application/x-www-form-urlencoded' && typeof value === 'object') {
// Handle URL-encoded data
const pairs: string[][] = [];
for (const key of Object.keys(value)) {
let val = value[key];
if (!(val instanceof Array)) {
val = [val];
}
for (const v of val) {
const formValue = this.formDataValue(v);
if (formValue !== null) {
pairs.push([key, formValue]);
}
}
}
this._bodyContent = pairs.map(p => `${encodeURIComponent(p[0])}=${encodeURIComponent(p[1])}`).join('&');
} else if (this._bodyContentType === 'multipart/form-data') {
// Handle multipart form data
const formData = new FormData();
if (value != null) {
Expand Down

0 comments on commit 56c469f

Please sign in to comment.