Reputation: 83
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.
Upvotes: 0
Views: 372
Reputation: 750
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:
service
declarations in your .proto and can get your clients generated for youAlso 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.
Upvotes: 2