3

I fetch a proto object from an api , is there anyway I can validate that the response is a proto object.

One thing which I did was check content-type as mentioned in the json counterpart of this question. But the answer goes on further to validate json using JSON.parse

The content-type of a proto file comes out to be application/octet-stream ( protobuf can have this content-type , check this )

Is there any other check I should do to validate response is a proto object ?

EDIT : Proto means Protocol Buffers : https://developers.google.com/protocol-buffers

Anonymous
  • 2,184
  • 15
  • 23
  • Are you referring to the __proto__ in javascript, or to a python class named proto? – levininja Apr 18 '18 at 18:44
  • 1
    Protocol Buffers : https://developers.google.com/protocol-buffers/ – Anonymous Apr 18 '18 at 18:48
  • Why not just try to `ParseFromString(response)`? If doesn't raise a `DecodeError` exception, then it passes validation. – cr3 Apr 18 '18 at 18:54
  • Going by the documentation , this could be a good check . Let me try this ! – Anonymous Apr 18 '18 at 19:08
  • @cr3 Thanks for the comment. But this requires to know the messageType of the proto file . Check [this](https://stackoverflow.com/questions/33816761/google-protocol-buffers-protobuf-in-python3-trouble-with-parsefromstring-en) for usage of `ParseFromString(response)`. Can there be a more general answer ? – Anonymous Apr 18 '18 at 19:27
  • @ShubhamSrivastava for a given endpoint, you should expect a single message type. If your endpoint can receive a handful of message types, you could loop over each type until one doesn't raise a `DecodeError` exception. If this part of the code can't determine any of the message types for some reason, then it might be acceptable to assume protobuf when the content-type is `application/octet-stream`. – cr3 Apr 18 '18 at 20:05
  • @cr3 I totally get your point , my case was that I have a single framework setup for parsing response for all api's in my project , and each api has different messageType in that case i guess checking only `content-type` would be helpful ! – Anonymous Apr 19 '18 at 06:28

1 Answers1

0

The Content-Type representation header is used to indicate the original media type of the resource (prior to any content encoding applied for sending). Meanwhile, protobuf is serialization/de-serialization schema/library. So they are different.

rjhcnf
  • 762
  • 6
  • 12