gRPC request -> unexpected token

I’m trying to test a gRPC service using K6.
The request is loaded from disk.
But when running the test, this error is happening:
GoError: unable to serialise request object to protocol buffer: proto: syntax error (line 1:1): unexpected token “{"greeting":"Bert"}”

What is wrong with the request? It seems to be valid JSON.

import grpc from 'k6/net/grpc';

const data = JSON.parse(open('./requests/hello.json'));
// console.log(data);
const client = new grpc.Client();
client.load(['definitions'], 'hello.proto');

export default () => {
    client.connect('localhost:9092', {
      plaintext: true
    });

    const response = client.invoke('hello.HelloService/SayHello', data);

    check(response, {
        'status is OK': (r) => r && r.status === grpc.StatusOK,
      });
    
      console.log(JSON.stringify(response.message));

      client.close();
};

Hi @ngruson

Welcome to the community forum :wave:

I’m not sure where the issue lies in your case. I tested a similar example:

import grpc from 'k6/net/grpc';
import { check, sleep } from 'k6';

const data = JSON.parse(open('./requests/hello.json'));
const client = new grpc.Client();

client.load(['definitions'], 'hello.proto');

export default () => {
    client.connect('grpcb.in:9000', {
        plaintext: true
    });

    const response = client.invoke('hello.HelloService/SayHello', data);

    check(response, {
        'status is OK': (r) => r && r.status === grpc.StatusOK,
    });

    console.log(JSON.stringify(response.message));

    client.close();

    sleep(1);
};

With the file in ./requests/hello.json

{
    "greeting": "Bert"
}

And the definitions/hello.proto

syntax = "proto2";

package hello;

service HelloService {
  rpc SayHello(HelloRequest) returns (HelloResponse);
  rpc LotsOfReplies(HelloRequest) returns (stream HelloResponse);
  rpc LotsOfGreetings(stream HelloRequest) returns (HelloResponse);
  rpc BidiHello(stream HelloRequest) returns (stream HelloResponse);
}

message HelloRequest {
  optional string greeting = 1;
}

message HelloResponse {
  required string reply = 1;
}

And the results look good:

I would start by testing the same example, which should work.

The differences can lie under your grpc service at localhost:9092. I would recommend you try to troubleshoot as described in GRPC load testing with K6 - #3 by eyeveebee - OSS Support - Grafana Labs Community Forums. The grpcurl command comes in handy to help you first test your endpoint. And if that works, we can compare it with what k6 is sending.

I hope this helps :bowing_woman:

Cheers!

Thank you for replying.
I got it to work now with a json file.
Not sure what I did wrong yesterday.

1 Like