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

Windows PowerShell中curl.exe执行POST请求返回JSON格式错误求助

PowerShell中curl.exe POST请求JSON格式错误的解决方法

错误命令

PS C:\Users\varda> curl.exe -X POST http://localhost:5000/tasks -H 'Content-Type:application/json' -d '{"title":"New Task","description":"This is my first task"}'

错误响应

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>SyntaxError: Expected property name or '}' in JSON at position 1<br> &nbsp; &nbsp;at JSON.parse (<anonymous>)<br> &nbsp; &nbsp;at parse (C:\Users\varda\Desktop\task manager\backend\node_modules\body-parser\lib\types\json.js:92:19)<br> &nbsp; &nbsp;at C:\Users\varda\Desktop\task manager\backend\node_modules\body-parser\lib\read.js:128:18<br> &nbsp; &nbsp;at AsyncResource.runInAsyncScope (node:async_hooks:206:9)<br> &nbsp; &nbsp;at invokeCallback (C:\Users\varda\Desktop\task manager\backend\node_modules\raw-body\index.js:238:16)<br> &nbsp; &nbsp;at done (C:\Users\varda\Desktop\task manager\backend\node_modules\raw-body\index.js:227:7)<br> &nbsp; &nbsp;at IncomingMessage.onEnd (C:\Users\varda\Desktop\task manager\backend\node_modules\raw-body\index.js:287:7)<br> &nbsp; &nbsp;at IncomingMessage.emit (node:events:518:28)<br> &nbsp; &nbsp;at endReadableNT (node:internal/streams/readable:1696:12)<br> &nbsp; &nbsp;at process.processTicksAndRejections (node:internal/process/task_queues:82:21)</pre>
</body>
</html>

问题根源

你用HTML转义实体&quot;代替了JSON要求的双引号,再加上PowerShell单引号的参数处理逻辑,导致这些实体无法被解析为正确的引号,服务器收到的请求体格式完全不符合JSON规范,自然解析失败。

修正后的命令

方案1:单引号包裹JSON内容(推荐)

curl.exe -X POST http://localhost:5000/tasks -H "Content-Type:application/json" -d '{"title":"New Task","description":"This is my first task"}'

PowerShell不会解析单引号内部的内容,会直接把原始的JSON字符串传递给curl.exe,服务器能正确识别格式。

方案2:双引号包裹并转义内部引号

curl.exe -X POST http://localhost:5000/tasks -H "Content-Type:application/json" -d "{\"title\":\"New Task\",\"description\":\"This is my first task\"}"

用反斜杠\转义双引号内的引号,PowerShell会将转义后的引号传递给curl.exe,生成符合要求的JSON请求体。

额外提示

  • PowerShell中curl默认是Invoke-WebRequest的别名,你直接调用curl.exe的做法是正确的,避免了命令冲突。
  • 发送JSON请求时,必须确保Content-Type头设置为application/json,否则服务器可能不会按JSON格式解析请求体。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 04:19:55