RESTFULL JSON API

GRPC

For example, define an API like this:

message SayHelloRequest {
    string stringValue = 1;
    int64 int64Value = 2;
}

message SayHelloResponse {
    string message = 1;
}

service TestService {
    rpc sayHello (SayHelloRequest) returns (SayHelloResponse) {}
}

Struct code generated by protobuf:

type SayHelloRequest struct {
       StringValue  string        `protobuf:"bytes,2,opt,name=stringValue" json:"stringValue,omitempty"`
       Int64Value   int64         `protobuf:"varint,3,opt,name=int64Value" json:"int64Value,omitempty"`
}

Refer to How to add a new API, implement this API.

Then, start server, send a POST request(Content-Type=application/json) with a body like this:

{
       "stringValue":"a string value",
       "int64Value":1234
}

NOTE: The key name in json MUST be the same with the name in tag “protobuf”.

You should be able to get the data in json in your grpc service func.

Thrift

Similar Example:

struct SayHelloRequest {
  1: string stringValue,
  2: i32 int32Value,
  3: bool boolValue,
}

struct SayHelloResponse {
  1: string message,
}

service TestService {
    SayHelloResponse testJson (1:SayHelloRequest request)
}

Struct code generated by thrift:

type TestJsonRequest struct {
  StringValue string `thrift:"stringValue,1" db:"stringValue" json:"stringValue"`
  Int32Value int32 `thrift:"int32Value,2" db:"int32Value" json:"int32Value"`
  BoolValue bool `thrift:"boolValue,3" db:"boolValue" json:"boolValue"`
}

Refer to How to add a new API, implement this API.

Then, start server, send a POST request(Content-Type=application/json) with a body like this:

{
       "StringValue":"a string value",
       "int32Value":1234,
       "boolvalue":true
}

NOTE: Different from grpc, the key names are case-insensitive in case of thrift.

You should be able to get the data in json in your thrift service func.