【发布时间】:2022-03-09 13:20:43
【问题描述】:
我是编码新手,并尝试在 Javascript 中使用我的第一个 API。我在弄清楚在哪里填充 API 密钥和标头时遇到了一些麻烦。它是一个 POST,用于向 IOT 设备发送小消息。引用为here,标头引用为here。
这是我所拥有的,但我已将我的 API 密钥替换为通用密钥。
request.open('POST', 'https://dashboard.hologram.io/api/1/devices/messages');
request.setRequestHeader('Content-Type', 'application/json');
request.onreadystatechange = function () {
if (this.readyState === 4) {
console.log('Status:', this.status);
console.log('Headers:', this.getAllResponseHeaders());
console.log('Body:', this.responseText);
}
};
var body = {
'deviceid': [storedDeviceID],
'protocol': 'TCP',
'port': 80,
'data': 'Hello world!',
'base64data': 'SGVsbG8gd29ybGQhCg=='
};
request.send(JSON.stringify(body));
我非常感谢任何帮助。谢谢。
【问题讨论】:
-
感谢您的帮助。