【发布时间】:2021-04-15 10:07:01
【问题描述】:
我在使用 content-type='application/x-protobuf' 发布数据时遇到问题。我们可以从 HTTP 请求中请求 protobuf 吗?如果可以怎么办?
【问题讨论】:
标签: node.js protocol-buffers proto
我在使用 content-type='application/x-protobuf' 发布数据时遇到问题。我们可以从 HTTP 请求中请求 protobuf 吗?如果可以怎么办?
【问题讨论】:
标签: node.js protocol-buffers proto
如果您对 protobuffer 进行编码和解码,这将只是另一个常规请求。
最好的选择是使用诸如this 之类的库进行编码和解码。
这是一个带有 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);
【讨论】:
content-type 通常作为标头传递。
您可以查看this answer 以获取有关如何在 POST 请求中传递标头的提示。
【讨论】: