Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to form protobuf resource part of http request body and test it through dhc client or postman for restful services

I have created a .proto message and I'm exposing a rest service which looks like this

@Path("/test")
public interface test{

@POST
@Produces("application/x-protobuf")
@Consumes("application/x-protobuf")
public Response getProperties(TestRequest testrq);
}

Now TestRequest being the Java generated file of .protobuf how do i pass it in request body ?

this will be be the .proto file format

message TestRequest
{
    string id = 1;
    string name = 2;
    enum TestType
    {
        Test=1
    }
   TestType testType = 3; 
}
like image 602
Rajiv Avatar asked Sep 04 '16 08:09

Rajiv


People also ask

Does Protobuf use HTTP?

Protobufs work fine over HTTP in their native binary format.

Does rest support Protobuf?

Yes, you can absolutely combine Protobuf and REST. Protbuf specifies a way to encode data. REST specifies a way to interact with resources, but does not require any particular encoding for the resource bodies.

What is Protobuf format?

Protocol buffers, or Protobuf, is a binary format created by Google to serialize data between different services. Google made this protocol open source and now it provides support, out of the box, to the most common languages, like JavaScript, Java, C#, Ruby and others.


2 Answers

Give Protoman a try if you need a simple protobuf api client.

Disclaimer: It's my side project

like image 130
spluxx Avatar answered Oct 03 '22 20:10

spluxx


You can use this code snippet to test the protobuf as i don't find any solution with postman or dhcclient

URL url = new URL("https://localhost:8080/test");
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setDoInput(true);
urlc.setDoOutput(true);
urlc.setRequestMethod("POST");
urlc.setRequestProperty("Accept", "application/x-protobuf");
urlc.setRequestProperty("Content-Type","application/x-protobuf");
TestRequestPb.TestRequest.Builder testRequestBuilder = TestRequestPb.TestRequest.newBuilder();
TestRequest testRequest = testRequestBuilder.build();
testRequest.writeTo(urlc.getOutputStream());

testRequest = TestRequest.newBuilder().mergeFrom(urlc.getInputStream()).build();
like image 33
Rajiv Avatar answered Oct 03 '22 21:10

Rajiv