Web requests via PowerShell

Sometimes the easiest way to create new web request is to get similar request and modify it. In Edge it is done by finding the web request and resending it (optionally editing it). This is one of the functionalities that I am missing in Crome. This is also available in Firefox (checked only the Dev edition, but probably also available in the regular). Some screenshots follow.

It is done using the GUI, it is easy to edit, the results are also easily saved, but sometimes it is not enough. When the web requests have to be modified via some script, then one of the easiest methods is to use PowerShell (assumimng in the Windows world or Linux with PS installed). There are different dedicated applications that are specialised in sending custom requests (Apache Bench, Curl, Wget, Postman, etc.), but installing additional software on the server is acceptable usually for test servers and not always for production servers. And even Edge is not installed on lot of servers. So sometimes a script or shell comand has to be used.

When using the Windows PowerShell ISE, the Commands GUI (last screenshot from the above gallery), the parameters can be also very conveniently edited and started.

The nearest of PS ISE on Linux is Visual Studio Code.

There is extensive documentation on the Microsoft site about the different parameters, that can be used, but this post will present just several of the.

The most basic and required parameter is the -Uri , which stands for Uniform Resource Identifier. This is the address, where the request has to be send.

The -Headers parameter is a little tricky, because it is has to be hash table or dictionary. So something like -Headers @{‘Accept’=’image/svg+xml,image/,/*;q=0.8’}

The default method is GET, but also the other methods can be selected using the -Method parameter.

The -Body of a post is also frequently used when posting some data to the server. Only POST and PUT require body.

So for example a simple request looks like (Windows Powershell 5)

Invoke-WebRequest -Uri https://demoandtest.website/non-wp_folder/server_checks/reply_headers.php -Headers @{'Accept'='text/html;q=0.9,image/webp,/;';'accept-language'='en-US,en;'} -Method GET -UserAgent test

For the PS Core (version 7 on Linux) the above request might not work and -SkipHeaderValidation might have to be added.

To test the headers the following link can be used https://demoandtest.website/non-wp_folder/server_checks/reply_headers.php to reply what headers were send to the server.

Sometimes it simplifies the life a lot, if the requests are generated by the PowerShell scripts- for example changing the Uri (from predefined list), passing different headers (for example language), changing the POST body (to instruct the server to provide different information), etc.

lia lia lia ... so much fun to add advertisement after the post
Scroll to Top