You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Golang gRPC Proto中award_map的Curl请求正确格式咨询

Golang Proto结构对应JSON POST请求格式错误解决

问题描述

我在Golang中定义了如下Proto结构及对应API路径,测试POST API调用时出现unexpected token错误,经确认该错误来自award_map字段(定义为map<string, People>,其中People为包含字符串数组的消息类型),请问Curl请求中award_map的正确数据格式是什么?

错误信息

{"code":3, "message":"proto: syntax error : unexpected token [","details":[]}

原POST请求示例

curl --header "Content-Type: application/json" \
     --request POST \
     --data '{"school_name":"MIT", "awards":["Summa-cum-laude", "Magna-cum-laude"], "award_map":{"Summa-cum-laude":[], "Magna-cum-laude": ["john", "mark"]}}' \
   <POST_ENDPOINT>

Proto定义

message People {
    repeated string names = 1;
}

message School {
  string school_name = 1;
  repeated string awards = 2;
  map<string, People> award_map = 3;
}

解决方案

问题出在award_map的JSON格式上:People是独立的Proto消息类型,不是直接的字符串数组,因此JSON中不能直接用[]赋值,必须把数组包裹在People对应的names字段里。

正确的award_map格式

"award_map": {
  "Summa-cum-laude": {"names": []},
  "Magna-cum-laude": {"names": ["john", "mark"]}
}

完整的正确Curl请求

curl --header "Content-Type: application/json" \
     --request POST \
     --data '{"school_name":"MIT", "awards":["Summa-cum-laude", "Magna-cum-laude"], "award_map":{"Summa-cum-laude":{"names":[]}, "Magna-cum-laude":{"names":["john", "mark"]}}}' \
   <POST_ENDPOINT>

原因说明

Proto的map<string, People>在JSON序列化时,每个值必须是People消息的JSON表示——即包含names字段的对象。原请求直接用数组赋值,违反了Proto转JSON的规范,因此触发语法错误。

内容的提问来源于stack exchange,提问作者alameda1232

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 03:50:31