Sunday, December 1, 2013

[protobuf] Sending protobuf serialized data using curl

Protocol buffer is may be really performant, but when it comes to debugging some APIs, it can be pretty much frustrating not to be able to use your old good usual tools.

One tool I love and use since my really first steps with http is curl, and here is how you can use it when dealing with protobuf payloads :

Assuming that

  - you already have protoc installed (Ex: yum install protobuf.x86_64)
  - the data you want to send is stored in clear text in file.msg
  - your proto file is file.proto
  - you want to encode it using the message type myPost

you can use the following set of commands to POST your data protobuf encoded :

cat file.msg | protoc --encode=myPost ./file.proto | curl -sS -X POST --data-binary @- http://hostname/api-route

---

Reading some protoBuffed output works exactly the same.

Imagine the preceding call to http://hostname/api-route responds some protobuffed output using the message type myResponse, you can use the following to output it in clear text

cat file.msg | protoc --encode=myPost ./file.proto | curl -sS -X POST --data-binary @- http://hostname/api-route | protoc --decode=myResponse ./file.proto


Enjoy

2 comments:

Unknown said...
This comment has been removed by the author.
Unknown said...
This comment has been removed by the author.