node.js - 如何使用内容类型 ='application/x-protobuf' 在 node.js 中发布数据
问题描述
我在使用 content-type='application/x-protobuf' 发布数据时遇到问题。我们可以从 HTTP 请求中请求 protobuf 吗?如果可以怎么办?
解决方案
如果您对 protobuffer 进行编码和解码,这将只是另一个常规请求。
最好的选择是使用这样的库进行编码和解码。
这是一个带有 proto 缓冲区的简单 axios 请求。
const protobuf = require('protobufjs');
...
const root = await protobuf.load('user.proto');
const User = root.lookupType('userpackage.User');
const postBody = User.encode({ name: 'Joe', age: 27 }).finish();
await axios.post('http://someaddress', postBody, {
headers: {
'content-type': 'application/x-protobuf'
}}).
then(res => res.data);
推荐阅读
- javascript - 在 Heroku 上部署 React 应用程序时出现应用程序错误
- npm - webpack捆绑我的代码后我需要依赖吗
- javascript - 从 AWS.DynamoDB.DocumentClient.Scan() 调用返回输出
- javascript - 如何在两个 nodejs 服务器之间传输文件?
- javascript - 访问某些路线时是否可以进行迷你(软)重新加载?
- android - 将 Google API 集成到 libgdx 项目中
- excel - 如何解决宏时间问题
- python - 大量图像文件夹到数据集
- c - 在 IF 命令范围内存储矩阵所需的内存仅在此范围内分配?
- python - 使用 Cognito 凭证的 AWS IOT 连接