我正在使用 Capacitor HTTP 插件发出 POST HTTP 请求,如下所示:
import { CapacitorHttp } from '@capacitor/core';
async test() {
const options = {
url: 'https://httpbin.org/post',
headers: {
'accept': 'application/json'
},
data: {
"foo": "bar"
},
};
const response = await CapacitorHttp.post(options);
console.log(response.data);
}
但是应用程序不会向服务器发送任何内容。请求正文为空。为什么?
确保将
Content-Type
请求标头设置为 application/json
:
const options = {
url: 'https://httpbin.org/post',
headers: {
'Content-Type': 'application/json',
'accept': 'application/json'
},
data: {
"foo": "bar"
},
};
const response = await CapacitorHttp.post(options);
console.log(response.data);