GithubHelp home page GithubHelp logo

zhutony / grpc-json-example Goto Github PK

View Code? Open in Web Editor NEW

This project forked from johanbrandhorst/grpc-json-example

0.0 2.0 0.0 2.03 MB

Example of using gRPC-Go with JSON as the transport encoding

License: MIT License

Makefile 1.88% Go 98.12%

grpc-json-example's Introduction

grpc-json-example

Example of using gRPC-Go with JSON as the transport encoding.

The JSON Codec

The included codec package contains an implementation of encoding.Codec for JSON payloads.

Server setup

The only thing necessary to make your gRPC server respond to gRPC requests encoded with JSON is to import the relevant package (to let it register with gRPC-Go's encoding registry):

import _ "github.com/johanbrandhorst/grpc-json-example/codec"

Request examples

gRPC client

Using a gRPC Client, simply initiate using the correct content-subtype as a grpc.DialOption:

grpc.WithDefaultCallOptions(grpc.CallContentSubtype(codec.JSON{}.Name()))

The included client shows a full example.

cURL

$ echo -en '\x00\x00\x00\x00\x17{"id":1,"role":"ADMIN"}' | curl -ss -k --http2 \
        -H "Content-Type: application/grpc+json" \
        -H "TE:trailers" \
        --data-binary @- \
        https://localhost:10000/example.UserService/AddUser | od -bc
0000000 000 000 000 000 002 173 175
         \0  \0  \0  \0 002   {   }
0000007
$ echo -en '\x00\x00\x00\x00\x17{"id":2,"role":"GUEST"}' | curl -ss -k --http2 \
        -H "Content-Type: application/grpc+json" \
        -H "TE:trailers" \
        --data-binary @- \
        https://localhost:10000/example.UserService/AddUser | od -bc
0000000 000 000 000 000 002 173 175
         \0  \0  \0  \0 002   {   }
0000007
$ echo -en '\x00\x00\x00\x00\x02{}' | curl -k --http2 \
        -H "Content-Type: application/grpc+json" \
        -H "TE:trailers" \
        --data-binary @- \
        --output - \
        https://localhost:10000/example.UserService/ListUsers
F{"id":1,"role":"ADMIN","create_date":"2018-07-21T20:18:21.961080119Z"}F{"id":2,"role":"GUEST","create_date":"2018-07-21T20:18:29.225624852Z"}

Explanation:

Using cURL to send requests requires manually adding the gRPC HTTP2 message payload header to the payload:

'\x00\x00\x00\x00\x17{"id":1,"role":"ADMIN"}'
#<-->----------------------------------------- Compression boolean (1 byte)
#    <-------------->------------------------- Payload size (4 bytes)
#                    <--------------------->-- JSON payload

Headers must include TE and the correct Content-Type:

 -H "Content-Type: application/grpc+json" -H "TE:trailers"

The string after application/grpc+ in the Content-Type header must match the Name() of the codec registered in the server.

The endpoint must match the name of the name of the proto package, the service and finally the method:

https://localhost:10000/example.UserService/AddUser

The responses are prefixed by the same header as the requests:

'\0  \0  \0  \0 002   {   }'
#<-->------------------------ Compression boolean (1 byte)
#    <------------>---------- Payload size (4 bytes)
#                    <---->-- JSON payload

grpc-json-example's People

Contributors

johanbrandhorst avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.