Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set the Host Header when using invoke-restmethod

For some reason this doesn't work

Invoke-RestMethod -Uri "http://localhost" -Headers @{"Host"="web.domain.com"}

I get the error

The 'Host' header must be modified using the appropriate property or method

But I can't find the method or property on how to do this.

Thanks

like image 664
Antony Denyer Avatar asked Nov 12 '13 18:11

Antony Denyer


People also ask

Can you set the host header?

You don't. You can't set HTTP headers as part of URL, it's not the way it works.

What is the difference between invoke-RestMethod and invoke-WebRequest?

Invoke-RestMethod is perfect for quick APIs that have no special response information such as Headers or Status Codes, whereas Invoke-WebRequest gives you full access to the Response object and all the details it provides.

What is Invoke-RestMethod?

Description. The Invoke-RestMethod cmdlet sends HTTP and HTTPS requests to Representational State Transfer (REST) web services that return richly structured data. PowerShell formats the response based to the data type. For an RSS or ATOM feed, PowerShell returns the Item or Entry XML nodes.


1 Answers

This is a bug that has been fixed in PowerShell version 4.0:

C:\PS> (irm http://localhost -Headers @{Host='web.domain.com'}).html

xmlns                                   head                                    body
-----                                   ----                                    ----
http://www.w3.org/1999/xhtml            head                                    body
like image 157
Keith Hill Avatar answered Oct 18 '22 17:10

Keith Hill