'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 1:[1]
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();
Solution 2:[2]
Solution 3:[3]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | FK82 |
Solution 2 | spluxx |
Solution 3 | Golly Ticker |