简体   繁体   中英

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

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

Disclaimer: It's my side project

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM