Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending protocol buffers via REST

I am trying to implement protocol buffers for client/server using REST. I am still a bit confused if I need to send protocol buffers request in byte format?

I mean, in my client code, do I need to serialize object to byte array? For example

protoRequest.build.toByteArray()

And in the server, do I need to c

   @POST
   @Consumes("application/octet-stream")
   public byte[] processProtoRequest(byte[] protoRequest) {
   ProtoRequest.Builder request = ProtoRequest.newBuilder();
   request.mergeFrom(protoRequest)
}

Is this the right thing to do?

Thanks

David

like image 385
user373307 Avatar asked Nov 06 '22 11:11

user373307


1 Answers

You can use input stream for this purpose. Server Side Code will be look like the below code

@POST
public Response processProtoRequest(@Context HttpServletRequest req) {
          ProtoRequest protoRequestObj = ProtoRequest.parseFrom(req.getInputStream());
          ///process  protoRequestObj and convert into byte arry and send to clinet
            return  Response.ok(protoRequestObj.toByteArray(),
                        MediaType.APPLICATION_OCTET_STREAM).status(200).build();

}

client side will look like this:

   ProtoRequest protoRequestObj = ProtoRequest.newBuilder(). //protocol buffer object
          setSessionId(id).
          setName("l070020").
          build();

       DefaultHttpClinet httpClinet = new DefaultHttpClinet();
       HttpPost request = new HttpPost("http://localhost:8080/maven.work/service/mainServices/protoRequest");
    request.addHeader("accept","application/octet-stream");
    request.setEntity(protoRequestObj.toByteArray());  
    HttpResponse response = httpClient.execute(request);
like image 165
Amir Qayyum Khan Avatar answered Nov 15 '22 07:11

Amir Qayyum Khan