Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any HTTP libraries in Powershell that is similar to Requests in Python or Mechanize in Ruby?

Are there any HTTP libraries in Powershell that is similar to Requests in Python or Mechanize in Ruby? What I want is a PowerShell library that can easily send GET, POST requests and easily manage cookies between requests. Does this kind of library exists? Thanks.

like image 361
Just a learner Avatar asked Mar 23 '23 13:03

Just a learner


2 Answers

I don't know either library you mention, but it sounds like something Invoke-WebRequest can do. You will need PowerShell v3 for that. Below is an example from the documentation of this command. Here and here are some other examples.

PS C:\> # Sends a sign-in request by running the Invoke-WebRequest cmdlet. The command specifies a value of "fb" for the SessionVariable parameter, and saves the results in the $r variable.

$r=Invoke-WebRequest http://www.facebook.com/login.php -SessionVariable fb

# Use the session variable that you created in Example 1. Output displays values for Headers, Cookies, Credentials, etc. 

$fb

# Gets the first form in the Forms property of the HTTP response object in the $r variable, and saves it in the $form variable. 

$form = $r.Forms[0]

# Pipes the form properties that are stored in the $forms variable into the Format-List cmdlet, to display those properties in a list. 

$form | Format-List

# Displays the keys and values in the hash table (dictionary) object in the Fields property of the form.

$form.fields

# The next two commands populate the values of the "email" and "pass" keys of the hash table in the Fields property of the form. Of course, you can replace the email and password with values that you want to use. 

$form.Fields["email"] = "[email protected]"
$form.Fields["pass"] = "P@ssw0rd"

# The final command uses the Invoke-WebRequest cmdlet to sign in to the Facebook web service. 

$r=Invoke-WebRequest -Uri ("https://www.facebook.com" + $form.Action) -WebSession $fb -Method POST -Body $form.Fields
like image 105
Lars Truijens Avatar answered Mar 25 '23 06:03

Lars Truijens


You seem to be looking for Invoke-WebRequest (requires PowerShell v3).

like image 29
Ansgar Wiechers Avatar answered Mar 25 '23 04:03

Ansgar Wiechers