Skip to content

Commit

Permalink
NXJS-218: fix multipart/form-data handling
Browse files Browse the repository at this point in the history
  • Loading branch information
troger committed Jan 7, 2025
1 parent f7ba568 commit 4dc1151
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions lib/operation.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,19 +108,22 @@ class Operation extends Base {
*/
execute(opts = {}) {
const options = this._computeOptions(opts);
options.headers = options.headers || {};
options.headers['Content-Type'] = this._computeContentTypeHeader(this._automationParams.input);
const body = this._computeRequestBody();
const headers = this._computeRequestHeaders(body);
const url = this._computeRequestURL();
let finalOptions = {
body,
headers,
url,
method: 'POST',
url: this._computeRequestURL(),
body: this._computeRequestBody(),
};
finalOptions = extend(true, finalOptions, options);
return this._nuxeo.http(finalOptions);
}

_computeContentTypeHeader(input) {
return this._isMultipartInput(input) ? 'multipart/form-data' : 'application/json';
_computeRequestHeaders(body) {
const contentType = body instanceof FormData ? body.getHeaders()['content-type'] : 'application/json';
return { 'Content-Type': contentType };
}

_computeRequestURL() {
Expand Down Expand Up @@ -167,7 +170,7 @@ class Operation extends Base {
context: this._automationParams.context,
};
const form = new FormData();
form.append('params', JSON.stringify(automationParams));
form.append('params', JSON.stringify(automationParams), { contentType: 'application/json' });

let inputIndex = 0;
for (const blob of input) {
Expand All @@ -186,7 +189,7 @@ class Operation extends Base {
context: this._automationParams.context,
};
const form = new FormData();
form.append('params', JSON.stringify(automationParams));
form.append('params', JSON.stringify(automationParams), { contentType: 'application/json' });
form.append('input', input.content, input.name);
return form;
}
Expand Down

0 comments on commit 4dc1151

Please sign in to comment.