Axios 解析错误:针对 Apple Push Sandbox API 请求时预期 HTTP/

问题描述 投票:0回答:3

我正在尝试在 Node.JS 环境中使用

POST https://api.sandbox.push.apple.com/messages/send/3/device/<TOKEN>
库执行
axios
。执行请求时,抛出以下错误:

Error: Parse Error: Expected HTTP/

当使用

http2
包尝试相同的请求时,它按预期工作,但我不想在我的项目中维护两个 http 请求包,并且我强烈更喜欢
axios
。这是缺少配置吗?

node.js axios
3个回答
0
投票

验证您是否在 axios 请求中设置了适当的标头。在 APNs(Apple 推送通知服务)的上下文中,您需要设置 apns-topic 标头。它应该看起来像这样:

const axios = require('axios');

const headers = {
  'apns-topic': 'your.bundle.id',
  'Authorization': `Bearer ${yourAuthenticationToken}`,
  'Content-Type': 'application/json',
};

const data = {
  // Your notification payload here
};

axios.post('https://api.sandbox.push.apple.com/3/device/<TOKEN>', data, { headers })
  .then(response => {
    // Handle the response
  })
  .catch(error => {
    // Handle errors
  });

0
投票

第 1 步确保您的客户端和服务器都支持 HTTP/2 至关重要 第 2 步:发出 HTTP 请求时,您需要使用正确的 HTTP/2 客户端库 步骤 3:发送 HTTP/2 请求 现在,我们以 Node js 为例详细了解一下如何发送 HTTP/2 请求:

host = 'https://api.sandbox.push.apple.com'
path = '/3/device/{you device token}'

const client = http2.connect(host);

client.on('error', (err) => console.error(err));

body = {
"aps": {
    "alert": "hello",
    "content-available": 1
}
}

headers = {
':method': 'POST',
'apns-topic': 'com.xxxxxx.xxxxxxx', //your application bundle ID
':scheme': 'https',
':path': path,
'authorization': `bearer ${token}`
}

const request = client.request(headers);

request.on('response', (headers, flags) => {
for (const name in headers) {
    console.log(`${name}: ${headers[name]}`);
}
});

request.setEncoding('utf8');
let data = ''
request.on('data', (chunk) => { data += chunk; });
request.write(JSON.stringify(body))
request.on('end', () => {
console.log(`\n${data}`);
client.close();
});
request.end();

-2
投票

尝试使用 HTTP 协议进行请求,如下所示:

http://api.sandbox.push.apple.com/messages/send/3/device/<TOKEN>
© www.soinside.com 2019 - 2024. All rights reserved.