Getting error while parsing from Proto message

30 views
Skip to first unread message

Dinesh Verma

unread,
Sep 12, 2022, 4:21:40 AMSep 12
to Protocol Buffers
Hi, 
I am writing backend service. The goal is to fetch proto-buf data from another API,  parse it and send back from my backend service. But I am getting some exceptions.

From another API: proto-buf message is received after sending header: "Accept: application/x-protobuf" and json format is received after sending header: "Accept: application/json".

In my backend service, I have proto files and also created corresponding java files as well. I want to return the response in proto and json as per Accept header after calling another API. I am getting exceptions as follows:-

Case-1: As per protobuf documentation -> 
<GeneratedJavaFile>.parseFrom(okHttp3 httpResponse.body.bytes())
Exceptioncom.google.protobuf.InvalidProtocolBufferException: While parsing a protocol message, the input ended unexpectedly in the middle of a field.  This could mean either that the input has been truncated or that an embedded message misreported its own length. 
Remark: Similar happened for inputStream as well If I try to convert response into byteStream().

Case-2:  
<GeneratedJavaFile>.Builder res = <GeneratedJavaFile>.newBuilder();
JsonFormat.parser().merge(okHttp3 httpResponse.body.string(), res);
Exception: Then also I am getting exception.

----
Kindly help as I have tried all other options available on net. Let me know if you need any other info.
Message has been deleted

Dinesh Verma

unread,
Sep 12, 2022, 4:28:51 AMSep 12
to Protocol Buffers
Case-3: If I try to cast the response to Message. So that I can use -> body.toByteArray(). Then I get ClassCastException. Is there any way where I can convert okHttp3 httpResponse.body() to protobuf Message class
Reply all
Reply to author
Forward
0 new messages