cURL
    rest-api

    cURL POST Request with JSON Body and Authorization Header

    Quick terminal guide on sending JSON POST payloads using cURL. Copy-paste examples for Bearer tokens, custom API keys, and Basic authentication headers.

    0 views
    Updated 7/6/2026

    Official Documentation Reference

    For in-depth explanations, configuration options, and standards compliance, visit the official documentation:

    Official cURL Documentation

    Ready to test this code?

    Load this example into the app

    Code Example

    Copy and run
    curl -X POST https://api.example.com/v1/users \
      -H "Content-Type: application/json" \
      -H "Authorization: Bearer YOUR_OAUTH_TOKEN_HERE" \
      -d '{
        "name": "Jane Doe",
        "email": "jane.doe@example.com",
        "role": "administrator"
      }'

    Overview

    cURL POST Request with JSON Body and Authorization Header

    When testing REST APIs in the terminal, sending a POST request with a JSON payload and an authentication header is one of the most common developer tasks. This reference guide provides copy-pasteable cURL commands for Bearer tokens, API keys, and Basic authentication, along with troubleshooting tips for Windows and Unix-like environments.

    1. Bearer Token Authentication (OAuth 2.0)

    Bearer tokens are the standard for secure API authorization. Use the -H flag to set the Authorization: Bearer <token> header, and -d to send the JSON string.

    curl -X POST https://api.example.com/v1/users \
      -H "Content-Type: application/json" \
      -H "Authorization: Bearer YOUR_OAUTH_TOKEN_HERE" \
      -d '{
        "name": "Jane Doe",
        "email": "jane.doe@example.com",
        "role": "administrator"
      }'
    

    2. Basic Authentication (Username & Password)

    For APIs using Basic Auth, cURL provides the -u flag to automatically base64-encode your credentials and append the Authorization: Basic <base64> header.

    curl -X POST https://api.example.com/v1/login \
      -u "username:password" \
      -H "Content-Type: application/json" \
      -d '{"client_id": "desktop_app"}'
    

    Alternatively, you can build the header manually: curl -H "Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=" ...

    3. Custom API Key Headers

    Many developer platforms (like Stripe, OpenAI, or AWS) authenticate requests using custom header keys (e.g., X-API-Key or Authorization).

    curl -X POST https://api.example.com/v1/data \
      -H "Content-Type: application/json" \
      -H "X-API-Key: secure_api_key_value" \
      -d '{"query": "analytics"}'
    

    Best Practices & Troubleshooting

    Handling Windows Command Prompt (cmd.exe) vs. Bash/Zsh

    On Unix (Linux/macOS), strings inside single quotes (') are preserved literally. However, the Windows Command Prompt (CMD) does not support single quotes for strings and requires double quotes, escaping the internal double quotes with a backslash (\"):

    Windows CMD Syntax:

    curl -X POST https://api.example.com/v1/users -H "Content-Type: application/json" -d "{\"name\": \"Jane Doe\"}"
    

    Windows PowerShell Syntax: PowerShell requires enclosing the JSON in single quotes, but double quoting internal fields:

    curl -X POST https://api.example.com/v1/users -H "Content-Type: application/json" -d '{"name": "Jane Doe"}'
    

    Formatting JSON Responses (jq)

    To pretty-print the JSON response directly in your terminal, pipe the cURL output to jq:

    curl -s -X POST https://api.example.com/v1/users -d '{"name":"Jane"}' | jq
    

    References

    Frequently Asked Questions

    Q: How do I view the response headers for debugging? A: Use the -i (include headers) or -I (headers only) flag in your cURL command to see server response headers, cookies, and HTTP status codes.

    Q: What is the difference between Bearer and Basic auth? A: Basic auth encodes a username and password directly. Bearer auth utilizes a cryptographic token (like a JWT) generated by an authentication server after successful login.

    Related Topics

    curl
    post
    json
    bearer-token
    authorization
    terminal

    Ready to test APIs like a pro?

    Apicurl is a free, powerful API testing tool.