Skip to content

Commit 3d1b12c

Browse files
OS-kepatotoricamarkemerjcesarmobile
authored
fix: http content headers not sent when using axios (#8064)
Co-authored-by: Mark Anderson <[email protected]> Co-authored-by: jcesarmobile <[email protected]>
1 parent e678825 commit 3d1b12c

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

android/capacitor/src/main/assets/native-bridge.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,8 @@ var nativeBridge = (function (exports) {
123123
};
124124
}
125125
else if (body instanceof FormData) {
126-
const formData = await convertFormData(body);
127126
return {
128-
data: formData,
127+
data: await convertFormData(body),
129128
type: 'formData',
130129
};
131130
}
@@ -657,7 +656,12 @@ var nativeBridge = (function (exports) {
657656
},
658657
});
659658
convertBody(body).then(({ data, type, headers }) => {
660-
const otherHeaders = this._headers != null && Object.keys(this._headers).length > 0 ? this._headers : undefined;
659+
let otherHeaders = this._headers != null && Object.keys(this._headers).length > 0 ? this._headers : undefined;
660+
if (body instanceof FormData) {
661+
if (!this._headers['Content-Type'] && !this._headers['content-type']) {
662+
otherHeaders = Object.assign(Object.assign({}, otherHeaders), { 'Content-Type': `multipart/form-data; boundary=----WebKitFormBoundary${Math.random().toString(36).substring(2, 15)}` });
663+
}
664+
}
661665
// intercept request & pass to the bridge
662666
cap
663667
.nativePromise('CapacitorHttp', 'request', {

core/native-bridge.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,8 @@ const convertBody = async (
106106
type: 'text',
107107
};
108108
} else if (body instanceof FormData) {
109-
const formData = await convertFormData(body);
110109
return {
111-
data: formData,
110+
data: await convertFormData(body),
112111
type: 'formData',
113112
};
114113
} else if (body instanceof File) {
@@ -323,7 +322,6 @@ const initBridge = (w: any): void => {
323322
const tagStyles = success
324323
? 'font-style: italic; font-weight: lighter; color: gray'
325324
: 'font-style: italic; font-weight: lighter; color: red';
326-
327325
c.groupCollapsed(
328326
'%cresult %c' + result.pluginId + '.' + result.methodName + ' (#' + result.callbackId + ')',
329327
tagStyles,
@@ -611,7 +609,6 @@ const initBridge = (w: any): void => {
611609

612610
window.XMLHttpRequest = function () {
613611
const xhr = new win.CapacitorWebXMLHttpRequest.constructor();
614-
615612
Object.defineProperties(xhr, {
616613
_headers: {
617614
value: {},
@@ -726,9 +723,18 @@ const initBridge = (w: any): void => {
726723
});
727724

728725
convertBody(body).then(({ data, type, headers }) => {
729-
const otherHeaders =
726+
let otherHeaders =
730727
this._headers != null && Object.keys(this._headers).length > 0 ? this._headers : undefined;
731728

729+
if (body instanceof FormData) {
730+
if (!this._headers['Content-Type'] && !this._headers['content-type']) {
731+
otherHeaders = {
732+
...otherHeaders,
733+
'Content-Type': `multipart/form-data; boundary=----WebKitFormBoundary${Math.random().toString(36).substring(2, 15)}`,
734+
};
735+
}
736+
}
737+
732738
// intercept request & pass to the bridge
733739
cap
734740
.nativePromise('CapacitorHttp', 'request', {

ios/Capacitor/Capacitor/assets/native-bridge.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,8 @@ var nativeBridge = (function (exports) {
123123
};
124124
}
125125
else if (body instanceof FormData) {
126-
const formData = await convertFormData(body);
127126
return {
128-
data: formData,
127+
data: await convertFormData(body),
129128
type: 'formData',
130129
};
131130
}
@@ -657,7 +656,12 @@ var nativeBridge = (function (exports) {
657656
},
658657
});
659658
convertBody(body).then(({ data, type, headers }) => {
660-
const otherHeaders = this._headers != null && Object.keys(this._headers).length > 0 ? this._headers : undefined;
659+
let otherHeaders = this._headers != null && Object.keys(this._headers).length > 0 ? this._headers : undefined;
660+
if (body instanceof FormData) {
661+
if (!this._headers['Content-Type'] && !this._headers['content-type']) {
662+
otherHeaders = Object.assign(Object.assign({}, otherHeaders), { 'Content-Type': `multipart/form-data; boundary=----WebKitFormBoundary${Math.random().toString(36).substring(2, 15)}` });
663+
}
664+
}
661665
// intercept request & pass to the bridge
662666
cap
663667
.nativePromise('CapacitorHttp', 'request', {

0 commit comments

Comments
 (0)