Skip to main content

How to Send Request Headers with Invoke-RestMethod in PowerShell

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

Request headers carry extra information with a web request, such as a token in Authorization. Invoke-RestMethod sends them from a hashtable you pass to -Headers. This guide sends headers to a small server that writes back what it receives, so you can see each one arrive, with real output from PowerShell 7.6.10.

The Invoke-RestMethod page on -Headers: "Specifies the headers of the web request. Enter a hash table or dictionary."

Syntax​

Syntax
Invoke-RestMethod <URL> -Headers @{ '<Name>' = '<value>' }

$headers = @{
'<Name>' = '<value>'
'<Name>' = '<value>'
}
Invoke-RestMethod <URL> -Headers $headers
PartWhat it means
<URL>The web address to send the request to
-HeadersThe parameter that takes the headers to send
@{ ... }A hashtable: pairs of a name and a value
'<Name>'A header's name, in quotes, such as Authorization
'<value>'The header's value, in quotes
$headersA variable of your own that holds the hashtable, handy for several headers

The about_Hash_Tables page: "Use an equal sign (=) to separate each key from its value." And: "Use a semicolon (;) or a line break to separate the key-value pairs."

The Example​

The example sends headers to a small web server on your own computer that writes back every header it receives, one per line. The request never leaves your computer, and the token in it, demo-token, is only a placeholder. The commands run in PowerShell 7.6.10 on a Mac, in a folder named headers-demo.

Prerequisites​

  • PowerShell 7. This guide uses PowerShell 7.6.10: $PSVersionTable.PSVersion.ToString() printed 7.6.10.
  • Python 3, for the example's echo server: python3 --version printed Python 3.15.2.

Step 1: Save the Echo Server​

In the headers-demo folder, save this Python script as echo_headers.py:

echo_headers.py
from http.server import BaseHTTPRequestHandler, HTTPServer


class EchoHeaders(BaseHTTPRequestHandler):
def do_GET(self):
text = "".join(f"{name}: {value}\n" for name, value in self.headers.items())
self.send_response(200)
self.send_header("Content-Type", "text/plain; charset=utf-8")
self.end_headers()
self.wfile.write(text.encode())


server = HTTPServer(("127.0.0.1", 8013), EchoHeaders)
print("Echoing request headers at http://127.0.0.1:8013/")
try:
server.serve_forever()
except KeyboardInterrupt:
print("\nStopped.")

The script starts a web server at http://127.0.0.1:8013/, an address that only programs on this computer can reach. For every GET request, do_GET turns the request's headers into lines of Name: value and sends them back as plain text. Ctrl+C stops it.

Step 2: Start the Echo Server​

Open a second PowerShell window, move to the headers-demo folder, and run the script:

PowerShell (second window)
python3 echo_headers.py
Output
Echoing request headers at http://127.0.0.1:8013/

Leave this window open: the server runs until Step 6 stops it.

Step 3: See the Headers PowerShell Sends by Itself​

Back in the first window, send a request without -Headers:

PowerShell
Invoke-RestMethod http://127.0.0.1:8013/
Output
Host: 127.0.0.1:8013
User-Agent: Mozilla/5.0 (Macintosh; macOS 26.7.4; en-US) PowerShell/7.6.10
Accept-Encoding: gzip, deflate, br

Even without -Headers, the request carried three headers. The Invoke-RestMethod page: "By default, the HTTP request includes default values for the following HTTP headers:", and it lists these three. Your User-Agent names your own system and PowerShell version.

Step 4: Send Your Own Headers​

PowerShell
$headers = @{
'Authorization' = 'Bearer demo-token'
'X-Api-Version' = '2'
}
Invoke-RestMethod http://127.0.0.1:8013/ -Headers $headers

macOS-style code window titled PowerShell where a hashtable saved in $headers holds two entries, Authorization set to Bearer demo-token and X-Api-Version set to 2, and Invoke-RestMethod sends it to http://127.0.0.1:8013/ with the Headers parameter.

Output
Host: 127.0.0.1:8013
Authorization: Bearer demo-token
X-Api-Version: 2
User-Agent: Mozilla/5.0 (Macintosh; macOS 26.7.4; en-US) PowerShell/7.6.10
Accept-Encoding: gzip, deflate, br

macOS-style terminal window titled PowerShell where the hashtable is typed over four lines, with >> at the start of each line after the first, and Invoke-RestMethod prints the five headers the server received, among them Authorization: Bearer demo-token and X-Api-Version: 2.

The first four lines build a hashtable with two entries and save it in $headers. While the braces were still open, PowerShell started each new line with >> and waited for the rest. -Headers $headers then turned each entry into a header: the key became the header's name and the value its value. Both arrived at the server, next to the three headers PowerShell adds by itself. In a real request, Authorization holds the token an API gave you; here demo-token stands in for it.

Step 5: Replace a Header PowerShell Adds​

PowerShell
Invoke-RestMethod http://127.0.0.1:8013/ -Headers @{ 'User-Agent' = 'demo-client/1.0' }
Output
Host: 127.0.0.1:8013
User-Agent: demo-client/1.0
Accept-Encoding: gzip, deflate, br

This time the hashtable is written in the command itself, with one entry. User-Agent is one of the headers PowerShell sends by itself, so the value from -Headers replaced PowerShell's own. The page: "Use the Headers parameter to add other headers or override the default values."

Step 6: Stop the Echo Server​

In the second window, press Ctrl+C:

Output
^C
Stopped.

The server stopped and the prompt came back. When you no longer need the example, delete the headers-demo folder.

Common Mistakes​

MistakeWhat happensDo this instead
A string instead of a hashtable: -Headers 'Authorization: Bearer demo-token'Invoke-RestMethod: Cannot bind parameter 'Headers'. Cannot convert the "Authorization: Bearer demo-token" value of type "System.String" to type "System.Collections.IDictionary".Pass a hashtable, as in Step 4
A comma between entries: @{ 'Authorization' = 'Bearer demo-token', 'X-Api-Version' = '2' }ParserError with The assignment expression is not valid. The input to an assignment operator must be an object that is able to accept assignments, such as a variable or a property.Separate entries with a semicolon or a line break
A name with a hyphen and no quotes: @{ X-Api-Version = '2' }ParserError with Missing '=' operator after key in hash literal.Put the name in quotes: 'X-Api-Version'

FAQ​

Can I write several headers on one line?​

Yes, with a semicolon between the entries: $headers = @{ 'Authorization' = 'Bearer demo-token'; 'X-Api-Version' = '2' } built the same hashtable as Step 4, and the server received the same two headers.

Does Invoke-WebRequest take the same headers?​

Yes, through the same parameter: (Invoke-WebRequest http://127.0.0.1:8013/ -Headers $headers).Content printed the same five lines as Step 4. The difference is in what comes back: Invoke-WebRequest returns a response object, and its Content property held the text the server sent.

Conclusion​

Invoke-RestMethod http://127.0.0.1:8013/ -Headers $headers sent Authorization: Bearer demo-token and X-Api-Version: 2, and the echo server showed both arriving. Put the headers in a hashtable, with each name in quotes and a semicolon or a line break between entries, and use the same parameter to replace a header PowerShell sends by itself.

Additional Resources​