fullstorydev / grpcurl

Like cURL, but for gRPC: Command-line tool for interacting with gRPC servers
MIT License
10.75k stars 502 forks source link

Support file input for request contents #321

Open t-katsumura opened 2 years ago

t-katsumura commented 2 years ago

Currently, grpcurl accept data input with -d string option. We can set request content directly to string part. If the value is '@', request contents are read from stdin.

So, we can set request content like this

grpcurl -d "{\"name\":\"sample user\"}" -proto ./helloworld.proto localhost:50051 sample.Greeter/SayHello

or

grpcurl -d @ -proto ./helloworld.proto localhost:50051 sample.Greeter/SayHello <<EOM
> {"name":"sample user"}
> EOM

or

grpcurl -d @ -proto ./helloworld.proto localhost:50051 sample.Greeter/SayHello < request_content.json

In this format, we have to keep options and file paths in different variables when writing some jobs in shell script. For example,

#!/bin/bash

options_array=()
options_array+=("-H 'name1: value1' -d @")
options_array+=("-H 'name2: value2' -d @")
options_array+=("-H 'name3: value3' -d @")
options_array+=("-H 'name4: value4' -d @")
options_array+=("-H 'name5: value5' -d @")

contents_array=()
contents_array+=("request_content1.json")
contents_array+=("request_content2.json")
contents_array+=("request_content3.json")
contents_array+=("request_content4.json")
contents_array+=("request_content5.json")

for ((i=0; i<"${#options_array[@]}"; i++)); do
    grpcurl ${options_array[${i}]} -proto ./helloworld.proto localhost:50051 sample.Greeter/SayHello < ${contents_array[${i}]}
done

So, I think the following format is better. This format is the same as curl command (https://man7.org/linux/man-pages/man1/curl.1.html)

grpcurl -d "{\"name\":\"sample user\"}" -proto ./helloworld.proto localhost:50051 sample.Greeter/SayHello

or

// get contents from stdin with '@' or '@-' (curl command's style)
grpcurl -d @- -proto ./helloworld.proto localhost:50051 sample.Greeter/SayHello <<EOM
> {"name":"sample user"}
> EOM

or

// file paths can be set just after '@'
grpcurl -d @request_content.json -proto ./helloworld.proto localhost:50051 sample.Greeter/SayHello

Then, we can write the same logic shown above simply.

#!/bin/bash

options_array=()
options_array+=("-H 'name1: value1' -d @request_content1.json")
options_array+=("-H 'name2: value2' -d @request_content2.json")
options_array+=("-H 'name3: value3' -d @request_content3.json")
options_array+=("-H 'name4: value4' -d @request_content4.json")
options_array+=("-H 'name5: value5' -d @request_content5.json")

for option in "${options_array[@]}"; do
    grpcurl ${option} -proto ./helloworld.proto localhost:50051 sample.Greeter/SayHello
done