我正在尝试使用 fetch 向 OneSignal REST API 发送 POST 请求:
var obj = {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
'app_id': '(API KEY)',
'contents': {"en": "English Message"},
'app_ids': ["APP IDS"],
'data': {'foo': 'bar'}
})
}
fetch('https://onesignal.com/api/v1/notifications', obj)
我知道你真的不应该把你的 API key 放在客户端代码中,但这只是一个测试,看看它是否有效。此外,我得到的错误不是来自服务器的错误响应,它是:
Possible Unhandled Promise Rejection (id: 0):
unsupported BodyInit type
我试过放一个
catch
fetch 上的方法,但它不会被调用。有点不知所措,不确定如何继续。
提前致谢!
最佳答案
即使我为 One-Signal REST API 尝试了相同的 POST 请求来创建通知,以下对我来说也很好。
const bodyObj = {
app_id: "**********",
included_segments: ["All"],
data: {"foo": "bar"},
contents: {"en": "Hi good morning"}
}
fetch('https://onesignal.com/api/v1/notifications',{
method:'POST',
headers:{
'Authorization':'Basic **********',
'Content-Type':'application/json'
},
body:JSON.stringify(bodyObj)
})
.then((response) => response.json())
.then((responseJson) => {
console.log("success api call");
})
.catch((error) => {
console.error(error);
});
关于post - react native 获取 "unsupported BodyInit type",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39056125/