fullstorydev / grpcurl

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

Correct syntax for -d in PowerShell? #106

Closed ptrckdev closed 3 years ago

ptrckdev commented 5 years ago

Hello, I am not sure if this is an issue with grpcurl or me not being able to use PowerShell.

I have a grpc java Server with Reflection turned on and am using this command to ping it:

grpcurl -plaintext localhost:50051 com.example.coffee.services.CoffeeMachine/Ping

This works just fine and it shows my response message in the Shell.

But when I try to add a request message with -d like shown in the Readme I keep getting invalid character Error

grpcurl -d '{"product":1}' -plaintext localhost:50051 com.example.coffee.services.CoffeeMachine/GetProduct

Error invoking method "com.example.coffee.services.CoffeeMachine/GetProduct": error getting request data: invalid character 'p' looking for beginning of object key string

When I throw the command in the git bash it does work.

This looked to me like a parsing issue, so I tried different types of quotes and escape characters to get it to work. I found some comment where one claimed that in Windows the executable handles character unspacing?

This type of escaping seems to work in CMD, but not in PowerShell

grpcurl -d "{\"product\":1}" -plaintext localhost:50051 com.example.coffee.services.CoffeeMachine/GetProduct

Would be really grateful to find out how to use this tool in PowerShell on Windows.

Thanks a lot!

jhump commented 5 years ago

@ptrckdev, this is a Power Shell question, not a grpcurl question. The idea is that the -d argument must be a single position argument to grpcurl. So, if you have whitespace therein, you need a way to escape the whitespace, which is shell dependent. A quick Google search for escaping in Power Shell comes up with this: http://www.rlmueller.net/PowerShellEscape.htm I would try that and see if it works. I don't have a Windows machine, so I can't try it myself.

jammerful commented 5 years ago

@ptrckdev I ran into the same issue, I worked around it by the following

$params = @{ "product" = 1 }
$params | ConvertTo-Json -Compress | gprcurl -d '@' -plaintext localhost:50051 com.example.coffee.services.CoffeeMachine/GetProduct
ashmind commented 3 years ago

Just as a note, I run into a similar issue and was able to resolve it by using both \ and single quotes around {}, e.g:

grpcurl  -d '{\"product\":1}'
TetsuroTakao commented 2 years ago

I aquired responce from grpcurl use command below in Windows command prompt. grpcurl -d "{\"product\":1}" -plaintext localhost:50051 com.example.coffee.services.CoffeeMachine/GetProduct

Use double quote wrapping json, no space in json, and escape double quote with back slash.

solstice333 commented 2 weeks ago

More about this here: https://stackoverflow.com/questions/6714165/powershell-stripping-double-quotes-from-command-line-arguments.