0

protobuf.js version: "protobufjs": "6.8.8",

i am new to protobufjs and planning to implement the protobufjs in Angular 2 project. I have installed the protobufjs using the "npm install protobufjs --save" inside the Angular 2 project(VS2015).

i have the service below, which sends the http request to Restful service with Content-Type: application/x-protobuf and get the response in protobuf format

getpersondetails(): Observable {
var headers = new Headers();
headers.append('Content-Type', 'application/x-protobuf');
return this.http.get('/system/data/getpersondetails', { headers });
}

Get the sample response as below, ↵*����Alex"(�0�↵&����Du���2355"

how to decode the response to below model object

//PerosnDetails.ts
export class PersonDetails
{
firstname:string;
lastname:string;
salary: number;
}

Can someone help me by providing some samples with descriptive steps for Get request in Angular 2 and typescript.

Any help are really appreciated.

1 Answer 1

2

You want the raw (binary) response, so you have to set the option responseType: "arraybuffer":

http.get('/system/data/getpersondetails', { 
  headers,
  responseType: "arraybuffer"
});

Then you can deserialize your message from the array buffer:

let details = PersonDetails.deserializeBinary(yourArrayBuffer);

But I recommend to use either gRPC-web or Twirp for two reasons:

  1. you can use service declarations in your .proto and can get your clients generated for you
  2. you have some standard for URL mapping and error handling

Also important: There are great alternatives to google-protobuf. For example ts-proto. Or protobuf-ts (this is a shameless plug, I am the author). It has support for Angular.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.