Powershell curl headers

Powershell Curl with Headers

In PowerShell, you can emulate the functionality of curl (a command-line tool for making HTTP requests) using the `Invoke-RestMethod` command. To include headers in your request, you need to pass them as part of the header hashtable.

$url = "https://api.example.com/endpoint"
$headers = @{
    "Content-Type" = "application/json"
    "Authorization" = "Bearer your_token"
}

$response = Invoke-RestMethod -Uri $url -Headers $headers

# Example headers usage:
$headersUsage = @{
    "User-Agent" = "Custom User Agent"
}

Invoke-RestMethod -Uri "https://api.example.com/headers-endpoint" -Headers $headersUsage

In the above example:

  • We define the URL to which the request will be sent: `$url = “https://api.example.com/endpoint”`
  • We define the headers using a hashtable: `$headers = @{ “Content-Type” = “application/json”; “Authorization” = “Bearer your_token” }`
  • We make the request using `Invoke-RestMethod`, passing the URL and headers as arguments: `$response = Invoke-RestMethod -Uri $url -Headers $headers`
  • We can also use a different set of headers for another request, as shown in the second example.

Leave a comment