使用LINE Messaging API创建Rich Menu时JSON解析错误求助
问题
在Windows 11的PowerShell中执行LINE官方文档的curl命令创建Rich Menu,调整了-d参数内JSON的size和name字段顺序后,请求返回400错误,提示「请求体无法解析为JSON(line: 1, column: 3)」。
执行的命令:
curl.exe -v -X POST https://api.line.me/v2/bot/richmenu -H 'Authorization: Bearer {my channel access token} -H 'Content-Type: application/json' -d '{"size":{"width":2500,"height":1686},"selected":false,"name":"Test the default rich menu","chatBarText":"Tap to open","areas":[{"bounds":{"x":0,"y":0,"width":1666,"height":1686},"action":{"type":"uri","label":"Tap area A","uri":"https://www.python.jp/train/index.html"}}]}'
完整返回信息:
Note: Unnecessary use of -X or --request, POST is already inferred. * Trying 147.92.249.148:443... * Connected to api.line.me (147.92.249.148) port 443 (#0) * schannel: disabled automatic use of client certificate * ALPN: offers http/1.1 * ALPN: server accepted http/1.1 * using HTTP/1.1 > POST /v2/bot/richmenu HTTP/1.1 > Host: api.line.me > User-Agent: curl/8.0.1 > Accept: */* > Authorization: Bearer 39agOWZRzGsJjTKMCHoaoIpjE5W9GDuQd0tT0RKv3Kdc9onMrqcwq758jAEztD+22f41jZtgdTO/FOfDi0GdxkcO63KCPRb0BrNCynQsokXJpM1XUMDQsKj7qW3StyfYxHwzVDQsCStwORWBH6ztQAdB04t89/1O/w1cDnyilFU= > Content-Type: application/json > Content-Length: 230 > < HTTP/1.1 400 Bad Request < server: legy < cache-control: no-cache, no-store, max-age=0, must-revalidate < content-type: application/json < date: Wed, 27 Sep 2023 23:24:26 GMT < expires: 0 < pragma: no-cache < x-content-type-options: nosniff < x-frame-options: DENY < x-line-request-id: 0a3b64be-2e00-4d90-9493-13338982f5e2 < x-xss-protection: 1; mode=block < content-length: 79 < {"message":"The request body could not be parsed as JSON (line: 1, column: 3)"}* Connection #0 to host api.line.me left intact
错误原因与解决方案
错误原因
- Authorization请求头未闭合:命令中第一个
-H参数的单引号缺失闭合标记,'Authorization: Bearer {my channel access token}后面没有对应的单引号,导致整个命令的参数解析混乱,最终发送给服务器的JSON格式完全错误。 - PowerShell引号解析冲突:PowerShell对单引号、双引号的解析逻辑与Linux终端不同,直接用单引号包裹含双引号的JSON内容,会引发解析异常,破坏JSON结构。
解决步骤
- 修复Authorization头的引号:给第一个
-H参数补充闭合的单引号,正确格式为:-H 'Authorization: Bearer {my channel access token}' - 调整JSON的引号处理方式:提供两种可行方案:
方案一:转义JSON内部双引号
用双引号包裹整个-d参数,同时将JSON内部的双引号转义为\",修复后的命令如下:
curl.exe -v -X POST https://api.line.me/v2/bot/richmenu -H 'Authorization: Bearer {my channel access token}' -H 'Content-Type: application/json' -d "{\"size\":{\"width\":2500,\"height\":1686},\"selected\":false,\"name\":\"Test the default rich menu\",\"chatBarText\":\"Tap to open\",\"areas\":[{\"bounds\":{\"x\":0,\"y\":0,\"width\":1666,\"height\":1686},\"action\":{\"type\":\"uri\",\"label\":\"Tap area A\",\"uri\":\"https://www.python.jp/train/index.html\"}}]}"
方案二:使用外部JSON文件
将JSON内容保存为单独的.json文件,避免引号冲突:
- 创建
richmenu.json文件,内容如下:
{ "size": {"width":2500,"height":1686}, "selected":false, "name":"Test the default rich menu", "chatBarText":"Tap to open", "areas":[ { "bounds":{"x":0,"y":0,"width":1666,"height":1686}, "action":{"type":"uri","label":"Tap area A","uri":"https://www.python.jp/train/index.html"} } ] }
- 执行curl命令读取该文件:
curl.exe -v -X POST https://api.line.me/v2/bot/richmenu -H 'Authorization: Bearer {my channel access token}' -H 'Content-Type: application/json' -d "@richmenu.json"
内容的提问来源于stack exchange,提问作者Akasama
相关产品推荐
相关产品推荐

