Overview
Generate cURL requests from OpenAPI specifications v2.0 and v3.0. This .NET CLI tool makes it easy to create HTTP request scripts from your Swagger/OpenAPI documentation.
Installation
Cargo
Install the Rust CLI when you already have the Rust toolchain (requires Rust 1.96+):
cargo install curlgenerator
macOS/Linux
Install the prebuilt CLI without the Rust toolchain:
curl -fsSL https://christianhelle.com/curlgenerator/install | bash
Windows PowerShell
Install the prebuilt CLI on Windows:
irm https://christianhelle.com/curlgenerator/install.ps1 | iex
Advanced install options
Install to a user-writable directory on macOS/Linux:
curl -fsSL https://christianhelle.com/curlgenerator/install \
| INSTALL_DIR="$HOME/.local/bin" bash
Pin a specific release tag on macOS/Linux:
curl -fsSL https://christianhelle.com/curlgenerator/install \
| VERSION="0.4.1" bash
Install to a custom directory on Windows:
$install = irm https://christianhelle.com/curlgenerator/install.ps1
& ([scriptblock]::Create($install)) `
-InstallDir "$env:USERPROFILE\bin"
Pin a specific release tag on Windows:
$install = irm https://christianhelle.com/curlgenerator/install.ps1
& ([scriptblock]::Create($install)) `
-Version "0.4.1"
Standalone CLI archives
Download prebuilt archives from GitHub Releases. Archives are available for Linux x64, macOS x64, macOS ARM64, and Windows x64. Windows on ARM currently uses the x64 standalone install path.
Legacy .NET tool
The original implementation is still distributed as a .NET Tool on NuGet.org:
dotnet tool install --global curlgenerator
Usage
USAGE:
curlgenerator [URL or input file] [OPTIONS]
EXAMPLES:
curlgenerator ./openapi.json
curlgenerator ./openapi.json --output ./
curlgenerator ./openapi.json --bash
curlgenerator ./openapi.json --output-type onefile
curlgenerator https://petstore.swagger.io/v2/swagger.json
curlgenerator https://petstore3.swagger.io/api/v3/openapi.json --base-url https://petstore3.swagger.io
curlgenerator ./openapi.json --authorization-header Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
curlgenerator ./openapi.json --azure-scope [Some Application ID URI]/.default
ARGUMENTS:
[URL or input file] URL or file path to OpenAPI Specification file
OPTIONS:
DEFAULT
-h, --help Prints help information
-v, --version Prints version information
-o, --output <o> ./ Output directory
--bash Generate Bash scripts
--no-logging Don't log errors or collect telemetry
--skip-validation Skip validation of OpenAPI Specification file
--authorization-header <HEADER> Authorization header to use for all requests
--content-type <CONTENT-TYPE> application/json Default Content-Type header to use for all requests
--base-url <BASE-URL> Default Base URL to use for all requests. Use this if the OpenAPI spec doesn't explicitly specify a server URL
--azure-scope <SCOPE> Azure Entra ID Scope to use for retrieving Access Token for Authorization header
--azure-tenant-id <TENANT-ID> Azure Entra ID Tenant ID to use for retrieving Access Token for Authorization header
Example Output
Running the following command:
curlgenerator https://petstore.swagger.io/v2/swagger.json
Outputs the following:
cURL Request Generator v0.1.1
Support key: mbmbqvd
OpenAPI statistics:
- Path Items: 14
- Operations: 20
- Parameters: 14
- Request Bodies: 9
- Responses: 20
- Links: 0
- Callbacks: 0
- Schemas: 67
Files: 20
Duration: 00:00:02.3089450
Which will produce the following files:
-rw-r--r-- 1 christian 197121 593 Dec 10 10:44 DeleteOrder.ps1
-rw-r--r-- 1 christian 197121 231 Dec 10 10:44 DeletePet.ps1
-rw-r--r-- 1 christian 197121 358 Dec 10 10:44 DeleteUser.ps1
-rw-r--r-- 1 christian 197121 432 Dec 10 10:44 GetFindPetsByStatus.ps1
-rw-r--r-- 1 christian 197121 504 Dec 10 10:44 GetFindPetsByTags.ps1
-rw-r--r-- 1 christian 197121 371 Dec 10 10:44 GetInventory.ps1
-rw-r--r-- 1 christian 197121 247 Dec 10 10:44 GetLoginUser.ps1
-rw-r--r-- 1 christian 197121 291 Dec 10 10:44 GetLogoutUser.ps1
-rw-r--r-- 1 christian 197121 540 Dec 10 10:44 GetOrderById.ps1
-rw-r--r-- 1 christian 197121 275 Dec 10 10:44 GetPetById.ps1
-rw-r--r-- 1 christian 197121 245 Dec 10 10:44 GetUserByName.ps1
-rw-r--r-- 1 christian 197121 513 Dec 10 10:44 PostAddPet.ps1
-rw-r--r-- 1 christian 197121 521 Dec 10 10:44 PostCreateUser.ps1
-rw-r--r-- 1 christian 197121 610 Dec 10 10:44 PostCreateUsersWithListInput.ps1
-rw-r--r-- 1 christian 197121 464 Dec 10 10:44 PostPlaceOrder.ps1
-rw-r--r-- 1 christian 197121 299 Dec 10 10:44 PostUpdatePetWithForm.ps1
-rw-r--r-- 1 christian 197121 274 Dec 10 10:44 PostUploadFile.ps1
-rw-r--r-- 1 christian 197121 513 Dec 10 10:44 PutUpdatePet.ps1
-rw-r--r-- 1 christian 197121 541 Dec 10 10:44 PutUpdateUser.ps1
Generated Script Example
In this example, the contents of PostAddPet.ps1 looks like this:
<#
Request: POST /pet
Summary: Add a new pet to the store
Description: Add a new pet to the store
#>
curl -X POST https://petstore3.swagger.io/api/v3/pet `
-H 'Accept: application/json' `
-H 'Content-Type: application/json' `
-d '{
"id": 0,
"name": "name",
"category": {
"id": 0,
"name": "name"
},
"photoUrls": [
""
],
"tags": [
{
"id": 0,
"name": "name"
}
],
"status": "available"
}'
Parameterized Scripts
The generated script will contain mandatory parameters for operations where the path contains parameters:
<#
Request: GET /pet/{petId}
Summary: Find pet by ID
Description: Returns a single pet
#>
param(
<# ID of pet to return #>
[Parameter(Mandatory=$True)]
[String] $petId
)
curl -X GET https://petstore3.swagger.io/api/v3/pet/$petId `
-H 'Accept: application/json' `
-H 'Content-Type: application/json'
Advanced Usage
Azure Integration
Here's an advanced example of generating cURL requests for a REST API hosted on Microsoft Azure that uses the Microsoft Entra ID service as an STS. For this example, use PowerShell and Azure CLI to retrieve an access token:
az account get-access-token --scope [Some Application ID URI]/.default `
| ConvertFrom-Json `
| %{
curlgenerator `
https://api.example.com/swagger/v1/swagger.json `
--authorization-header ("Bearer " + $_.accessToken) `
--base-url https://api.example.com `
--output ./HttpFiles
}
You can also use the --azure-scope and --azure-tenant-id arguments which internally use DefaultAzureCredentials from the Microsoft.Extensions.Azure NuGet package:
curlgenerator `
https://api.example.com/swagger/v1/swagger.json `
--azure-scope [Some Application ID URI]/.default `
--base-url https://api.example.com `
--output ./HttpFiles