Search code examples
javarestpostmanprotocol-buffersdhclient

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; 
}

Solution

  • 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();