如何在Golang中将JSON请求的主体传递给使用映射字符串数据结构的API?

huangapple go评论139阅读模式
英文:

How do I pass a json body request to api in a map string data structure in Golang?

问题

我是你的中文翻译助手,以下是翻译好的内容:

我对golang和grpc还不熟悉,需要指导和澄清。我有以下定义作为调用外部API的POST请求的参数。

  1. params := map[string]string{
  2. "movie": movie,
  3. "seat": seat,
  4. "pax": fmt.Sprint(pax),
  5. "class": class,
  6. }

在proto文件中,我有以下定义:

  1. message TicketData {
  2. string movie = 1;
  3. string seat = 2;
  4. uint32 pax = 3;
  5. string class = 4;
  6. }
  7. message SearchMovieRequest {
  8. TicketData data = 1;
  9. }

然而,在POSTMAN(grpc请求)中,请求体显示如下:

  1. {
  2. "data": {
  3. "movie": "abc",
  4. "seat": "123",
  5. "pax": 2,
  6. "class ": "b""
  7. }
  8. }

请求体应该如下所示:

  1. {
  2. "data": [
  3. {
  4. "movie": "abc",
  5. "seat": "123",
  6. "pax": 2,
  7. "class ": "b""
  8. }
  9. ]
  10. }

我的JSON请求体中缺少方括号。我尝试使用structpb和map string interface,但似乎不起作用。任何指针将不胜感激。谢谢。

英文:

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.

  1. params := map[string]string{
  2. "movie": movie,
  3. "seat": seat,
  4. "pax": fmt.Sprint(pax),
  5. "class": class,
  6. }

In proto file, I have below:

  1. message TicketData {
  2. string movie= 1;
  3. string seat= 2;
  4. uint32 pax= 3;
  5. string class = 4;
  6. }
  7. message SearchMovieRequest {
  8. TicketData data= 1;
  9. }

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

  1. {
  2. "data":
  3. {
  4. "movie": "abc",
  5. "seat": "123",
  6. "pax": 2,
  7. "class ": "b""
  8. }
  9. }

the request body should be below:

  1. {
  2. "data": **[**
  3. {
  4. "movie": "abc",
  5. "seat": "123",
  6. "pax": 2,
  7. "class ": "b""
  8. }
  9. **]** - missing brackets in my json body
  10. }

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

答案1

得分: 2

你想要将data字段设置为repeated TicketData

请参考Protobuf [Language Guide (proto3)]中的Specifying Field Rules

具体来说:

  1. message TicketData {
  2. string movie= 1;
  3. string seat= 2;
  4. uint32 pax= 3;
  5. string class = 4;
  6. }
  7. message SearchMovieRequest {
  8. repeated TicketData data= 1;
  9. }

> 注意 虽然你包含了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:

  1. message TicketData {
  2. string movie= 1;
  3. string seat= 2;
  4. uint32 pax= 3;
  5. string class = 4;
  6. }
  7. message SearchMovieRequest {
  8. repeated TicketData data= 1;
  9. }

> 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.

huangapple
  • 本文由 发表于 2022年6月24日 18:26:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/72742650.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定