how to pass string array in grpc call using nextjs

Viewed 149

I'm making a request to my backend grpc endpoint

However on the backend, i get this error and the array is being set to null but the size is being populated:

2022-06-07 11:37:43.507 ERROR [pool-6-thread-5] c.s.p.commons.ExceptionHandler           : handleThrowable call failed: 
java.lang.IllegalArgumentException: argument is null or empty

However when i trigger the request to the backend endpoint from my terminal with an array everything is working.

My proto:

message LogRequest {
  repeated string object_uuid = 1;
  optional int32 size = 2;
}

Frontend Call:

const getChangelogs = async () => {
      const test = JSON.stringify({
        object_uuid : ["3", "4"],
        size: sizeNo
      });
      const response = await fetch('/api/log', {
        method: 'post',
        body: test,
      });
      const json = await response.json();
      ...
    };

Next JS API Call:

export default (_req: NextApiRequest, res: NextApiResponse) => {
    getLoggerServiceClient().getLogsForObjects(
    JSON.parse(_req.body),
    (err, response) => {
      if (err) {
        logger.warn(`error getLogsForObjects: ${JSON.stringify(err)}`);
        return res.status(500).json({ error: err });
      }
      logger.info(
        `success getLogsForObjects:  ${JSON.stringify(response)}`,
      );
      return res.status(response ? 500 : 200).json(response);
    },
  );
};

I've tried various ways of setting the JSON body but i still get the same issue, atm the JSON body is set to this and not sure if its the wrong syntax:

{ 
    "object_uuid":["3","4"],"size":2
}
0 Answers
Related