2

I have a server using NestJs+gRPC, I storage data in PostgreSQL, there is no problems in getting data and so on. I can't send grpcurl request :((

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.connectMicroservice<MicroserviceOptions>(grpcClientOptions);

  await app.startAllMicroservicesAsync();
  await app.listen(3000);
  console.log(`Application is running on: ${await app.getUrl()}`);
}
(async () => await bootstrap())();

export const grpcClientOptions: ClientOptions = {
  transport: Transport.GRPC,
  options: {
    url: '0.0.0.0:5000',
    package: 'user',
    protoPath: join(__dirname,'user/user.proto'),
    loader: {
      keepCase: true,
      longs: Number,
      defaults: false,
      arrays: true,
      objects: true,
    },
  },
};

Proto file looks like

syntax = "proto3";

package user;

service UserService {
  rpc FindOne (UserById) returns (User) {}
}

message UserById {
  string id = 1;
}

message User {
  int32 id = 1;
  string name = 2;
  string password = 3;
  string email = 4;
  string createdAt = 5;
  string updatedAt = 6;
}

And user controller

  @Get(':id')
  getById(@Param('id') id: string): Observable<User> {
    console.log(id);
    return this.userService.findOne({ id : +id });
  }

  @GrpcMethod('UserService','FindOne')
  async findOne(data: UserById): Promise<User> {
    const { id } = data;
    console.log(id);
    return this.userModel.findOne({
      where: {
        id : id
      },
    });
  }

It works correctly when I sending request from browser, but I can't make it using grpcurl. enter image description here

Thanks in forward!

Alex Kirillov
  • 35
  • 2
  • 7
  • Please copy-paste text into questions rather than include images. The commands looks well-formed and, for me in a Linux shell, I get (as expected) a connection refused (as I've no server on localhost:5000). I suspect (!) the issue is that you're on Windows but using a forward-slash between `src/user` whereas on Windows (!?) file path separators should be a back-slash? – DazWilkin Sep 22 '20 at 15:53
  • The url `0.0.0.0:5000` doesn't look right for a client. What if you use `localhost:5000` instead? – murgatroid99 Sep 22 '20 at 17:58
  • @DazWilkin Thanks a lot!!!! Now I can send requests, but now I got another problem: I can't pass my id:1 param. In terminal I have grpcurl -d "{"id":1}" -plaintext -import-path src\user -proto user.proto localhost:5000 user.UserService.FindOne Error invoking method "user.UserService.FindOne": error getting request data: invalid character 'i' looking for beginning of object key string. May be u can help? I'm using Windows – Alex Kirillov Sep 22 '20 at 19:06
  • You're welcome! Glad it worked. Please try `-d "{\"id\":1}"` – DazWilkin Sep 22 '20 at 19:30
  • @DazWilkin grpcurl -d '{\"id\":1}' -plaintext -import-path src\user -proto user.proto localhost:5000 user.UserService.FindOne Error invoking method "user.UserService.FindOne": error getting request data: invalid character '\'' looking for beginning of value . It doens't work, but thank u anyway)) – Alex Kirillov Sep 23 '20 at 06:16
  • OK, please see the link and try `-d '{\"id\": 1}'` https://learn.microsoft.com/en-us/aspnet/core/grpc/test-tools?view=aspnetcore-3.1 – DazWilkin Sep 23 '20 at 16:12
  • @DazWilkin thanks, can u pls put your comment as answer and I'll accept it – Alex Kirillov Sep 28 '20 at 11:01

2 Answers2

3

I suspect (!) the issue is that you're on Windows but using a forward-slash (/) between src/user whereas on Windows (!?) file path separators should be a back-slash (\).

please see this link on Microsoft Test gRPC services with gRPCurl and try -d '{\"id\": 1}'.

DazWilkin
  • 32,823
  • 5
  • 47
  • 88
  • The space character should also be removed, to make the command works correctly on Windows. – Xieyi Jul 06 '22 at 07:19
3

Grpcurl on winodws differes from Linux/Mac. On winodws, no need to enclose a json message single quote. E.g.

grpcurl.exe --plaintext -d {\"message\":\"How\u0020are\u0020you\"} localhost:9090  GreetingService.greeting

Note: We need to escape whitespace using \u0020 and escape double quotes with a back slash'\'.

ouflak
  • 2,458
  • 10
  • 44
  • 49
KHEMRAJD
  • 41
  • 4