Skip to main content

How to Send a POST Request to a Web API with curl and PowerShell

· 8 min read
Jagdish Kumawat
Founder @ Dewiride

A POST request sends data to a web API: a new order, a message, a reminder. You don't need a separate tool to send one while you build or test an API. curl does it on macOS and Linux, and PowerShell has Invoke-RestMethod built in. This guide sends the same JSON request both ways, reads the status code, and shows the one header you can't leave out.

Syntax​

Terminal
curl -X POST <url> -H "Content-Type: application/json" -d '<json>'
PartWhat it is
-X POSTThe HTTP method
<url>The address of the API endpoint
-H "Content-Type: application/json"A request header that says the body is JSON
-d '<json>'The body, in single quotes so the shell leaves the double quotes inside alone
-i (optional)Also print the response's status line and headers

The Example​

The API for this guide is a small ASP.NET Core minimal API with one endpoint: POST /orders takes an order as JSON and answers 201 Created with the order and its new ID. This is all of its Program.cs:

Program.cs
var builder = WebApplication.CreateBuilder(args);

var app = builder.Build();

app.MapPost("/orders", (OrderRequest order) =>
Results.Created("/orders/1001", new { id = 1001, order.Item, order.Quantity }));

app.Run();

record OrderRequest(string Item, int Quantity);

The OrderRequest parameter tells ASP.NET Core to read the request's body as JSON. The commands on this page were run with curl 8.7 on macOS and PowerShell 7.6.

Prerequisites​

  • The .NET SDK, to run the example API. dotnet --version should print a version number; this page used 10.0.404.

Step 1: Start the API​

In the API's project folder, start it on a fixed port, so the commands below work as written:

Terminal
dotnet run --urls http://localhost:5080
Output
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://localhost:5080
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.

Leave it running, and open a second terminal for the requests.

Step 2: Send the POST Request​

Terminal 2
curl -X POST http://localhost:5080/orders -H "Content-Type: application/json" -d '{"item":"Coffee beans","quantity":2}'
Output
{"id":1001,"item":"Coffee beans","quantity":2}

The API read the order and answered with it, plus its new id. curl prints the response exactly as it arrived. Invoke-RestMethod turned the JSON into an object and printed it as a table.

Step 3: See the Status Code and Headers​

A web API says how the request went with a status code, and the response's headers carry more, such as where the new order lives.

Add -i:

Terminal 2
curl -i -X POST http://localhost:5080/orders -H "Content-Type: application/json" -d '{"item":"Coffee beans","quantity":2}'
Output
HTTP/1.1 201 Created
Content-Type: application/json; charset=utf-8
Date: Tue, 24 Nov 2026 07:20:30 GMT
Server: Kestrel
Location: /orders/1001
Transfer-Encoding: chunked

{"id":1001,"item":"Coffee beans","quantity":2}

A macOS-style terminal window: curl -i -X POST to localhost 5080 orders with the Content-Type application/json header and the coffee beans order, answered by HTTP/1.1 201 Created, the Location header /orders/1001 and the order as JSON

201 Created means the API created something, and its Location header says where.

Step 4: See What Happens Without the Content Type​

Leave out the header and send the same body:

Terminal 2
curl -i -X POST http://localhost:5080/orders -d '{"item":"Coffee beans","quantity":2}'
Output
HTTP/1.1 415 Unsupported Media Type
Content-Length: 0
Date: Tue, 24 Nov 2026 07:20:31 GMT
Server: Kestrel

415 Unsupported Media Type: the API refused the request because of its label, not its content. Both tools label a POST without an explicit content type as a form. curl's manual says -d sends data using the content-type application/x-www-form-urlencoded. PowerShell's documentation says the same of Invoke-WebRequest and Invoke-RestMethod: For a POST method, the content type is application/x-www-form-urlencoded. Microsoft's ASP.NET Core documentation lists what happens when a minimal API endpoint that reads a JSON body gets anything else: Wrong content type (not application/json), status code 415.

So send the header every time you send JSON.

Common Mistakes​

MistakeWhat happensDo this instead
Leaving out the content type415 Unsupported Media TypeAdd -H "Content-Type: application/json" or -ContentType "application/json"
Double quotes around the JSON in bash400 Bad Request: the shell removes the quotes inside, so the body isn't valid JSONPut the JSON in single quotes
Using curl in Windows PowerShell 5.1In that version, curl is an alias of Invoke-WebRequest, with different parametersUse Invoke-RestMethod, or type curl.exe

Troubleshooting​

SymptomLikely causeFix
curl: (7) Failed to connect to localhost port 5080The API isn't running, or listens on another portStart it, and check the address in its output
400 Bad RequestThe body isn't valid JSON, or doesn't match what the endpoint expectsCheck the quotes and the property names
404 Not FoundWrong pathCheck the endpoint's route, such as /orders

FAQ​

Why do the two tools show the answer differently?​

curl prints the bytes of the response. Invoke-RestMethod reads the JSON and gives you a PowerShell object, which PowerShell prints as a table. You can use its properties, such as (Invoke-RestMethod ...).id.

How do I send a POST without a body?​

Leave out -d or -Body. curl then sends no content type at all. PowerShell still labels the request as a form, so add -ContentType "application/json" if the endpoint accepts only JSON.

Conclusion​

A POST request needs four things: the method, the address, the body, and a content type that says what the body is. curl takes them as -X POST, the URL, -d and -H "Content-Type: application/json". PowerShell's Invoke-RestMethod takes them as -Method Post, -Uri, -Body and -ContentType. Leave out the content type, and an API that expects JSON answers 415.

Additional Resources​