我正在尝试向我的GCP IoT设备发送新配置。 Base64编码的二进制字符串的长度约为15k字节。根据{{3}},GCP IoT设备配置大小限制为64k。但是我仍然收到413(请求实体太大)错误。我究竟做错了什么? 15k似乎很小,不会产生这样的错误。感谢您的帮助。
以下是发送配置数据的JavaScript代码:
sendDeviceConfig(deviceId, configPayload) {
const parentName = `projects/${this.projectId}/locations/${this.cloudRegion}`;
const registryName = `${parentName}/registries/${this.registryId}`;
const binaryData = Buffer.from(configPayload).toString('base64');
const request = {
name: `${registryName}/devices/${deviceId}`,
versionToUpdate: 0,
binaryData: binaryData,
};
return new Promise((resolve, reject)=>{
this.client.projects.locations.registries.devices.modifyCloudToDeviceConfig(
request,
(err) => {
if (err) {
this.logger.error('Could not update config:', deviceId);
reject(err);
} else {
resolve();
}
}
);
});
}
...以及部分HTML格式的(wtf?)错误响应:
<html lang=en>
<meta charset=utf-8>
<meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
<title>Error 413 (Request Entity Too Large)!!1</title>
...
</style>
<a href=//www.google.com/><span id=logo aria-label=Google></span></a>
<p><b>413.</b> <ins>That’s an error.</ins>
<p>Your client issued a request that was too large.
答案 0 :(得分:1)
我认为使用base64编码的有效载荷约为22.4kb。
但是,如果配置超过16kb并放入请求标头中,则Google将返回413。它应该位于Post的正文中。
答案 1 :(得分:0)
看来旧版客户端库可能在做一些奇怪的事情,下面的代码(用作示例代码的插入)对我来说适用于较大的配置负载:
const iot = require('@google-cloud/iot');
const newclient = new iot.v1.DeviceManagerClient({
// optional auth parameters.
});
const parentName = `projects/${projectId}/locations/${cloudRegion}`;
const registryName = `${parentName}/registries/${registryId}`;
const binaryData = Buffer.from(data).toString('base64');
const request = {
name: `${registryName}/devices/${deviceId}`,
binaryData: binaryData,
};
newclient.modifyCloudToDeviceConfig(request)
.then(responses => {
const response = responses[0];
// doThingsWith(response)
})
.catch(err => {
console.error(err);
});