如何将JSON主体请求传递给Golang的地图字符串数据结构中的API?

发布于 2025-02-10 08:20:40 字数 920 浏览 3 评论 0原文

我是Golang和GRPC的新手,需要指导和澄清。我有以下定义作为参数来调用外部API的发布请求。

    params := map[string]string{
    "movie":       movie,
    "seat":         seat,
    "pax": fmt.Sprint(pax),
    "class":      class,
}

在原始文件中,我有以下内容:

message TicketData {
    string movie= 1;
    string seat= 2;
    uint32 pax= 3;
    string class = 4;
}

message SearchMovieRequest {
    TicketData data= 1;
}

但是,在Postman(GRPC请求)中,正文请求如下:

{
    "data": 
        {
            "movie": "abc",
            "seat": "123",
            "pax": 2,
            "class ": "b""
        }
   
}

请求主体应在下面:

{
    "data": **[**
        {
            "movie": "abc",
            "seat": "123",
            "pax": 2,
            "class ": "b""
        }
    **]** - missing brackets in my json body
}

我尝试使用structpb并映射字符串接口。它似乎不起作用。任何指针都将不胜感激。谢谢。

I am new to golang and grpc, need guidance and clarification. I have below definition as a parameter to call a POST request for an external API.

    params := map[string]string{
    "movie":       movie,
    "seat":         seat,
    "pax": fmt.Sprint(pax),
    "class":      class,
}

In proto file, I have below:

message TicketData {
    string movie= 1;
    string seat= 2;
    uint32 pax= 3;
    string class = 4;
}

message SearchMovieRequest {
    TicketData data= 1;
}

However in POSTMAN (grpc request), the body request is showing below:

{
    "data": 
        {
            "movie": "abc",
            "seat": "123",
            "pax": 2,
            "class ": "b""
        }
   
}

the request body should be below:

{
    "data": **[**
        {
            "movie": "abc",
            "seat": "123",
            "pax": 2,
            "class ": "b""
        }
    **]** - missing brackets in my json body
}

I have tried using structpb and also map string interface. It doesn't seem to work. Any pointer will be appreciated. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城月光淡如水﹏ 2025-02-17 08:20:40

您希望数据字段为重复ticketdata

请参阅例如“ nofollow noreferrer”>指定字段规则://developers.google.com/protocol-buffers/docs/proto3“ rel =“ nofollow noreferrer”>语言指南(proto3)

具体来说:

message TicketData {
    string movie= 1;
    string seat= 2;
    uint32 pax= 3;
    string class = 4;
}

message SearchMovieRequest {
    repeated TicketData data= 1;
}

注意尽管您包括Protobuf定义,但您的示例是JSON。 Protobuf实现通常包括Protobuf和JSON之间的自动映射,这是 - 我认为 - 您正在展示的内容。

You want the data field to be repeated TicketData.

See e.g. Specifying Field Rules in the Protobuf Language Guide (proto3).

Specifically:

message TicketData {
    string movie= 1;
    string seat= 2;
    uint32 pax= 3;
    string class = 4;
}

message SearchMovieRequest {
    repeated TicketData data= 1;
}

NOTE Although you include protobuf definitions, your examples are JSON. Protobuf implementations usually include automatic mappings between protobuf and JSON which is -- I assume -- what you're presenting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文