Search code examples
grpcnestjsgrpcurl

Can't send a request using grpcurl


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!


Solution

  • 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}'.