---
title: "Windows curl JSON Escaping: cmd, PowerShell, Git Bash, -d @file"
description: "Windows curl JSON errors usually come from shell quoting. Get working cmd, PowerShell and Git Bash syntax, plus the safer -d @file.json method and next steps."
url: https://cdnping.org/en/blog/windows-curl-json-quote-escape
language: en
published: 2026-11-28
category: "Troubleshooting"
tags: ["curl","Windows","PowerShell","JSON","API calls","troubleshooting"]
source: cdnping.org
license: Free to quote and summarize with a link to the url above.
---
# 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?

| Environment | Outer quotes | Double quotes inside the JSON | Line continuation |
| --- | --- | --- | --- |
| cmd | Double quotes only | Write as `\"` | `^` |
| PowerShell | Single and double quotes both work, but behave differently when passed to external programs | Depends on version; error-prone | Backtick `` ` `` |
| Git Bash | Single quotes are the easiest | Write 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:

```bat
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)](/en/blog/windows-set-env-variable-api-key)); 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:

```powershell
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:

```powershell
$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:

```bash
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.

## Why is -d @file.json recommended?

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.

```json
{
  "model": "your-model",
  "messages": [
    { "role": "user", "content": "你好" }
  ]
}
```

2. Send it with `-d @body.json`. In cmd:

```bat
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.

3. 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:

- 400 with a JSON complaint: go back and check the quotes, `-Depth`, and the file encoding.
- 401: the key is wrong or in the wrong place; see [401 Invalid API Key: a 6-item checklist](/en/blog/api-401-invalid-api-key).
- 404 or a "model does not exist" message: usually the endpoint path or the model name; see [How to tell whether an endpoint URL works: 3 commands to test it yourself](/en/blog/test-openai-compatible-endpoint).
- No HTTP status code, only connection-type errors: this is a network-layer problem, not a JSON problem. For TLS handshake failures, see [curl SSL_ERROR_SYSCALL: a checklist for TLS handshake failures when calling AI APIs](/en/blog/ssl-error-syscall-tls-handshake-fail). For the differences between refused, timed out, and reset, see [connection refused vs timed out: adding reset, and how to tell which layer is failing from the error and the timing](/en/blog/connection-refused-timeout-reset-diff).
- Any other status code: check [LLM API error code cheatsheet: what 400 to 529 mean and which ones to retry](/en/blog/ai-api-error-codes-400-to-529-cheatsheet).

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](/en/blog/first-ai-api-call-tutorial) and then come back to the Windows details. To check whether the network path is fine, you can use the [cdnping online speed test](/en) to see first-connection and round-trip times for various AI endpoints in your browser, and the [API status page](/en/status) 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](https://agiplan.dev/models) for specifics. If you want to try it, start with the [setup guide](https://agiplan.dev/startup), 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.
