user1860447
user1860447

Reputation: 1436

How to pass in json as payload in .proto

As per the following page I should be able to send in json payload : https://developers.google.com/protocol-buffers/docs/proto3 under 'JSON Mapping'.

I would like to send in json payload as part of the message and I have the following .proto file :

message EventsRequest{
    message RequestElement {
        struct payload = 1;
    }
    string customerId = 1;
    repeated RequestElement jsonPayload = 2;
}


message EventsResponse {
    int32 status = 1;
    string rawResponseData = 2;
    struct responseData = 3;
}

But compiling it gives me the following error :

[INFO] Compiling 1 proto file(s) to C:\workspace\...\target\generated-sources\protobuf\java
[ERROR] PROTOC FAILED: msg_service.proto:21:9: "struct" is not defined.
msg_service.proto:34:5: "struct" is not defined.

[ERROR] C:\workspace\...\src\main\proto\msg_service.proto [0:0]: msg_service.proto:21:9: "struct" is not defined.
msg_service.proto:34:5: "struct" is not defined.

I have tried 'Struct' also, but I got the same error.

Am I misunderstanding usage ? If I have to send in json payload, do I pass in as a string ?

Thanks

Upvotes: 6

Views: 9754

Answers (5)

Srijan singh
Srijan singh

Reputation: 11

I think the issue is it is not able to import the right message from google/protobuf/struct.proto , so I used

google.protobuf.Struct field_name = 1

and it worked for me !!!

Upvotes: 1

dchau
dchau

Reputation: 41

If you would like to use a Struct you will need to first import:

import "google/protobuf/struct.proto";

Then during the declaration instead of just the word Struct use google.protobuf.Struct

Upvotes: 3

Aravin
Aravin

Reputation: 7067

It should be like this,

syntax = "proto3";

package db;
import "google/protobuf/struct.proto";

service Proxy
{
    rpc appConfig(UserId) returns (AppConfig);
}

message UserId
{
    string userId= 1;
}

message AppConfig
{
    Struct appConfig = 1;
}


Upvotes: 1

user1860447
user1860447

Reputation: 1436

I finally ended up using String to represent json payload.

Upvotes: 13

Eric Anderson
Eric Anderson

Reputation: 26394

It should be Struct, with a capital S.

Upvotes: 0

Related Questions