9

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; 
}
Rajiv
  • 179
  • 1
  • 2
  • 16

3 Answers3

8

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

Disclaimer: It's my side project

spluxx
  • 81
  • 2
  • 2
4

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();
FK82
  • 4,907
  • 4
  • 29
  • 42
Rajiv
  • 179
  • 1
  • 2
  • 16
  • Postman works fine with binary protobuf request body. Just use `protoc` or your app code to generate the binary request body as a local file, then in your Postman GET to the endpoint, make sure you have `Content-Type application/protobuf` in the headers, and for the Body select `binary` and then select your protobuf binary file. – yokeho Apr 21 '21 at 00:02
1

You can use protoCURL, a command line tool, to send Protobuf payloads to your HTTP REST endpoints and debug it. It works just like with cURL - except that you can use the Protobuf text format or JSON format to describe your request, which will be automatically encoded and decoded. Since the accepted answer does not actually use postman or dhc client, I think that a CLI might be acceptable for you or other readers.

In your case the request would look like this:

protocurl -I path/to/proto -i ..TestRequest -o ..TestResponse \
  -u http://localhost:8080/test \
  -d "id: 'myid', name: 'myname', testType: Test"

assuming your .proto file is in the folder path/to/proto.

However, because your .proto file does not specify the response type TestResponse, it's not possible to display it. You would need to add a response message to your .proto file. There is however an open issue for displaying responses even without its message schema.

You can find examples for outputs in the repository.

Disclaimer: I am the creator of this and made it, because I encountered the same problem.