[python] How to add REST API support using google.api.http?

690 views
Skip to first unread message

MAX IKON

unread,
Jan 14, 2022, 12:18:39 PM1/14/22
to grpc.io

Can you please tell me how to add REST API support using google.api.http?syntax = "proto3";

import "google/api/annotations.proto"; service TestService { rpc Method(MethodRequest) returns (MethodResponse) { option (google.api.http) = { post: "/api/v1/test" body: "*" }; } }
What should be added here?
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) stt_pb2_grpc.add_TestServiceServicer_to_server( TestService(), server ) address = f"{host}:{port}" server.add_insecure_port(address) server.start() server.wait_for_termination()

Lidi Zheng

unread,
Jan 19, 2022, 5:29:11 PM1/19/22
to grpc.io
Hi,

The REST API annotation is not a native feature of gRPC. Using gRPC Python (based on the snippet) won't be able to spawn an HTTP/1.1 server with the given interface.

However, there are multiple projects that can handle the HTTP/1.1 and HTTP/2 (gRPC) translation for you. For example: https://github.com/grpc-ecosystem/grpc-gateway
Reply all
Reply to author
Forward
0 new messages