2

I'm having problem in post data with content-type='application/x-protobuf'. Can we request protobuf from HTTP request? if can how to do it?

Charlie
  • 22,886
  • 11
  • 59
  • 90
Aminudin
  • 109
  • 2
  • 8

2 Answers2

4

It is going to be just another regular request if you encode and decode protobuffer.

The best option is to use a library such as this for encoding and decoding.

Here is a simple axios request with proto buffer.

  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);
Charlie
  • 22,886
  • 11
  • 59
  • 90
0

content-type is usually passed as a header.

You could see this answer for a hint on how to pass headers on a POST request.

Amit
  • 387
  • 3
  • 14