Windows curl JSON Escaping: cmd, PowerShell, Git Bash, -d @file

Blog / Troubleshooting

Windows curl JSON Escaping: cmd, PowerShell, Git Bash, -d @file

When curl keeps failing on JSON in Windows, the problem is usually not curl. It is cmd, PowerShell, or Git Bash altering your quotes before they ever reach curl. The most reliable fix is to put the JSON in a file and send it with -d @body.json, which works the same way in all three environments.

Below, we cover why the common errors happen, then give the correct syntax for each environment. The endpoint URLs, model names, and keys in this article are placeholders; replace them with your own, and check your provider's official documentation for the exact fields.

Why does the same curl command fail on Windows?

Typical errors fall into a few groups:

  • The server returns 400 with a JSON parsing complaint (invalid JSON, Unexpected token, and so on; the wording varies by service). The request went out, but the quotes in the body were already mangled.
  • PowerShell complains about a missing parameter, or you get errors mentioning Invoke-WebRequest. In Windows PowerShell 5.1, curl is an alias for Invoke-WebRequest, not the real curl.
  • cmd says 'xxx' is not recognized as an internal or external command. This usually means the command used Linux-style single quotes and the \ line continuation, neither of which cmd understands.

Most tutorials are written for bash on Linux/macOS: single quotes around the JSON and a trailing \ to continue lines. Moving that style to cmd or PowerShell almost always breaks.

What are the quoting rules in each environment?

EnvironmentOuter quotesDouble quotes inside the JSONLine continuation
cmdDouble quotes onlyWrite as \"^
PowerShellSingle and double quotes both work, but behave differently when passed to external programsDepends on version; error-proneBacktick `
Git BashSingle quotes are the easiestWrite as-is\

PowerShell deserves one more note. When you pass arguments to an external program such as curl.exe, the older version (5.1) strips the double quotes inside the string, so you have to escape them extra. Newer PowerShell 7 releases changed this behavior, but it differs between minor versions. Since the behavior depends on the version, don't bet on it; the file approach in a later section avoids the problem entirely.

How do I write it in cmd?

In cmd, wrap the whole JSON in double quotes and put a backslash before each double quote inside it:

curl https://api.example.com/v1/chat/completions ^
  -H "Content-Type: application/json" ^
  -H "Authorization: Bearer %API_KEY%" ^
  -d "{\"model\":\"your-model\",\"messages\":[{\"role\":\"user\",\"content\":\"你好\"}]}"

Three things to watch: there must be no space after the ^ continuation character; %API_KEY% requires that you set the environment variable first (see How to set an environment variable to store your API key on Windows (PowerShell / GUI)); and if the JSON contains characters such as % or &, cmd may interpret them on its own, which is one more reason to use the file approach.

How do I write it in PowerShell?

First, call curl.exe with the extension to avoid the alias:

curl.exe https://api.example.com/v1/chat/completions `
  -H "Content-Type: application/json" `
  -H "Authorization: Bearer $env:API_KEY" `
  -d '{\"model\":\"your-model\",\"messages\":[{\"role\":\"user\",\"content\":\"你好\"}]}'

The whole JSON is wrapped in single quotes, and the inner double quotes get an extra backslash. This is the form needed for version 5.1, which eats quotes. If you are on a newer PowerShell that behaves differently, this form may end up with stray backslashes, so the file approach is still the one to prefer.

The other route is to skip curl and use PowerShell's built-in Invoke-RestMethod, which converts an object to JSON so you never write the quotes by hand:

$body = @{
  model    = "your-model"
  messages = @(@{ role = "user"; content = "你好" })
} | ConvertTo-Json -Depth 5

Invoke-RestMethod -Uri "https://api.example.com/v1/chat/completions" `
  -Method Post `
  -Headers @{ Authorization = "Bearer $env:API_KEY" } `
  -ContentType "application/json" `
  -Body $body

ConvertTo-Json expands only two levels by default, so a nested messages array needs a larger -Depth; otherwise it gets flattened into a string and the server will reject it too. This is the most common trap in PowerShell.

How do I write it in Git Bash?

Git Bash is bash, so the syntax matches Linux tutorials:

curl https://api.example.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $API_KEY" \
  -d '{"model":"your-model","messages":[{"role":"user","content":"你好"}]}'

If the JSON itself contains single quotes, either switch to a file or build the string with escaping in bash. The latter is easy to get wrong.

When the JSON lives in a file, the shell never gets a chance to touch the quotes. Steps:

  1. Create body.json with valid JSON, saved as UTF-8. In Notepad, choose UTF-8 under Save As; don't pick the other encoding options, or Chinese text may come out garbled.
{
  "model": "your-model",
  "messages": [
    { "role": "user", "content": "你好" }
  ]
}
  1. Send it with -d @body.json. In cmd:
curl https://api.example.com/v1/chat/completions -H "Content-Type: application/json" -H "Authorization: Bearer %API_KEY%" -d @body.json

In PowerShell, change curl to curl.exe and %API_KEY% to $env:API_KEY. In Git Bash, use $API_KEY. The rest is the same.

  1. Note that @ is a special character in PowerShell. If you hit a parsing problem, wrap it in quotes: -d "@body.json".

The file approach also makes debugging easier: confirm the file is valid with any JSON validator or editor first, then send the request.

Still getting errors after the request goes out?

Work out which layer the error is in, then handle it accordingly:

Add -v to see the request headers curl actually sends and the connection process, and -i to see the response headers. Before pasting output to anyone else, mask your key.

Is there an easier starting point for your first API call?

If you haven't gotten a first request working yet, walk through Your first AI API call: sending a request with curl and then come back to the Windows details. To check whether the network path is fine, you can use the cdnping online speed test to see first-connection and round-trip times for various AI endpoints in your browser, and the API status page to see whether the official endpoints have had recent problems.

A quick disclosure: AGI Plan is a commercial partner of cdnping. AGI Plan is a monthly-subscription gateway that aggregates large models from multiple providers, paid in RMB with no overseas credit card needed. It is compatible with both the OpenAI and Anthropic API formats, so any tool that accepts a base URL plus an API key can connect, and for tools like Claude Code and Codex you change just one endpoint address. Note that it is not an officially authorized channel, and some models are marked as available only outside China; see the models page for specifics. If you want to try it, start with the setup guide, which has three steps: register with an emailed verification code, pick a plan and create a key, and change one setting.

Summary and next steps

  • On Windows, prefer -d @body.json; it works the same in all three environments.
  • In PowerShell, remember to use curl.exe, or switch to Invoke-RestMethod and set -Depth.
  • Confirm the JSON is valid before you suspect the key or the network.

Next step: create body.json, send your first request with the commands above, and if you get an HTTP error code, jump to the matching article in the list above to keep digging.

Related articles

Keep reading