Aminudin
Aminudin

Reputation: 129

How to post data in node.js with content type ='application/x-protobuf'

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?

Upvotes: 2

Views: 3513

Answers (2)

Charlie
Charlie

Reputation: 23798

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);

Upvotes: 4

Amit
Amit

Reputation: 397

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.

Upvotes: 0

Related Questions