Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

grpc response cannot be decoded #796

Closed
yangchnet opened this issue Sep 20, 2024 · 2 comments
Closed

grpc response cannot be decoded #796

yangchnet opened this issue Sep 20, 2024 · 2 comments

Comments

@yangchnet
Copy link

I have a grpc request:

GRPC 127.0.0.1:8090/ListenerDiscoveryService/FetchListeners

{
    "node": {
        "id": "xxx",
        "metadata": {
            "type": "proxy"
        }
    },
    "type_url": "type.googleapis.com/envoy.config.endpoint.v3.ClusterLoadAssignment"
}

This is a steam rpc request to envoy xds-server,its definition can be found here: https://github.com/envoyproxy/data-plane-api/blob/main/envoy/service/listener/v3/lds.proto

The response of this request:

{
  "versionInfo": "2024-09-20T19:59:36+08:00/138",
  "resources": [
    {
      "type_url": "type.googleapis.com/envoy.config.listener.v3.Listener",
      "value": {
        "type": "Buffer",
        "data": [
          10,
          7,
          115,
          117,
          45,
          116,
          101,
          ...
         ]
      }
    },
}     

When I use postman to request, it returns the following:

{
    "resources": [
        {
            "type_url": "type.googleapis.com/envoy.config.endpoint.v3.ClusterLoadAssignment",
            "value": "ChdvdXRib3VuZHwzMDAwfHx0ZXN0MDYyOQ=="
        }
     ]
}

I tried decoding it with a script but I got gibberish:

{{+after

    const jsonData = JSON.parse(response.body);
    // console.info(jsonData.resources[0].value.data)

    jsonData.resources.forEach(resource => {
        const decodedValue = Buffer.from(resource.value.data, 'base64').toString('utf8');
        resource.value = decodedValue;
        console.info(decodedValue);
    });
    
}}

console log:

INFO: 
�owt.pdl.test��
��
241.0.0.47��	������
-envoy.filters.network.http_connection_manager"��

How can I turn it into a readable json?

@AnWeber
Copy link
Owner

AnWeber commented Nov 3, 2024

resource.value seems already to be a Buffer. You just need to use .toString("utf-8")

{{+after

    const jsonData = JSON.parse(response.body);
    // console.info(jsonData.resources[0].value.data)

    jsonData.resources.forEach(resource => {
        const decodedValue = resource.value.toString('utf8');
        resource.value = decodedValue;
        console.info(decodedValue);
    });
    
}}

postman apparently does not use JSON.stringify directly but automatically converts all buffers into a base64 object. Maybe I should also offer such a behavior

@AnWeber
Copy link
Owner

AnWeber commented Nov 3, 2024

With next release type will be a base64 string

@AnWeber AnWeber closed this as completed Nov 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants