Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Current events
Recent changes
Random page
Help
Categories
LemonWiki共筆
Search
Search
Appearance
Log in
Personal tools
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Troubleshooting of curl errors in Mandarin
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Windows 與 Mac/Linux 系統的 cURL JSON 傳遞問題 === '''1. 問題''' 在 Windows 系統執行 cURL 命令時遇到以下錯誤: <pre> Invalid JSON payload received. Unknown name "": Root element must be a message. </pre> '''2. 解決方案''' Windows 系統: <pre> curl -X POST -H "Content-Type: application/json" "API_URL" -d "{\"key\":\"value\"}" </pre> Mac/Linux 系統: <pre> curl -X POST -H "Content-Type: application/json" "API_URL" -d '{"key":"value"}' </pre> PHP 程式碼: <pre> $is_windows = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'; if ($is_windows) { $command = sprintf('-d "{\"key\":\"%s\"}"', $value); } else { $command = sprintf("-d '%s'", $json_payload); } </pre> '''3. 問題說明''' * 根本原因:Windows 和 Mac/Linux 處理 shell 命令中的引號方式不同 * Windows:需要使用雙引號並轉義內部的引號 * Mac/Linux:可以使用單引號包住完整的 JSON 字串 * 當使用不當的引號格式時,JSON payload 會被錯誤解析,導致 API 回傳格式錯誤的訊息
Summary:
Please note that all contributions to LemonWiki共筆 are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see
LemonWiki共筆:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)