Skip to content

Commit 08ec4a5

Browse files
committed
curl syntax generation for file upload
1 parent 78be81f commit 08ec4a5

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/components/api-request.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -419,23 +419,30 @@ export default class ApiRequest extends LitElement {
419419

420420
//Form Params
421421
if (formParamEls.length>=1){
422+
422423
let formEl = requestPanelEl.querySelector("form");
423424
if (formEl.classList.contains("form-urlencoded")){
424425
let formUrlParams = new URLSearchParams();
425426
fetchOptions.headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8'
426427
curlHeaders = curlHeaders + ` -H "Content-Type: application/x-www-form-urlencoded"`;
427-
formParamEls.map(function(el){
428-
if (el.value){
429-
formUrlParams.append(el.dataset.pname,el.value);
430-
curlForm = curlForm + ` -F "${el.dataset.pname}=${el.value}"`;
431-
}
432-
});
433428
fetchOptions.body = formUrlParams;
434429
}
435-
else{
436-
//fetchOptions.headers['Content-Type'] = 'multipart/form-data; charset=utf-8'
430+
else {
431+
//fetchOptions.headers['Content-Type'] = 'multipart/form-data' // Dont set content type for fetch, coz the browser must auto-generate boundry value too
432+
curlHeaders = curlHeaders + ` -H "Content-Type: multipart/form-data"`;
437433
fetchOptions.body = new FormData(formEl);
438434
}
435+
436+
formParamEls.map(function(el){
437+
if (el.value){
438+
if (el.type !== 'file'){
439+
curlForm = curlForm + ` -F "${el.dataset.pname}=${el.value}"`;
440+
}
441+
else{
442+
curlForm = curlForm + ` -F "${el.dataset.pname}=@${el.value}"`;
443+
}
444+
}
445+
});
439446
}
440447

441448
//Body Params (json/xml/text)

0 commit comments

Comments
 (0)