【问题标题】:How to post data in node.js with content type ='application/x-protobuf'如何使用内容类型 ='application/x-protobuf' 在 node.js 中发布数据
【发布时间】:2021-04-15 10:07:01
【问题描述】:

我在使用 content-type='application/x-protobuf' 发布数据时遇到问题。我们可以从 HTTP 请求中请求 protobuf 吗?如果可以怎么办?

【问题讨论】:

    标签: node.js protocol-buffers proto


    【解决方案1】:

    如果您对 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);
    

    【讨论】:

      【解决方案2】:

      content-type 通常作为标头传递。

      您可以查看this answer 以获取有关如何在 POST 请求中传递标头的提示。

      【讨论】:

        猜你喜欢
        • 2016-05-30
        • 2017-07-06
        • 2018-04-04
        • 1970-01-01
        • 2019-07-28
        • 2016-06-15
        • 2014-04-26
        • 2020-05-04
        • 2021-01-01
        相关资源
        最近更新 更多