By using this site you agree to the use of cookies by Brugbart and our partners.

Learn more

Making HTTP POST Requests With AutoIt

This Tutorial will focus on post requests in AutoIt, using the Winhttprequest.5.1 object.

Edited: 2016-01-03 01:29

Last: Making HTTP GET Requests with AutoIt Next: Setting Request Headers With AutoIt

AutoIt Logo

Performing a HTTP POST Request in AutoIt does not really require anymore steps than making a get request, but there are still some details that we would like to cover in this Tutorial. We will not cover the details from the last AutoIt tutorial, so if you have not read it yet, you may want to do so.

To make things easier, we will consider a HTML form on a website – this form can be anything, as long as you have all the information required to perform a successful request from your AutoIt script. To test if your script works, you may attempt to send your request to Brugbarts Test page, which will be provided later in this tutorial.

Do not attempt to make a desktop client for your favorite website right away, do some basic requests first, so that you are sure you know what you are doing.

The AutoIt Post Request

When you want to make a HTTP POST request with AutoIt, all you have to do is to use the POST value in the Open method – the open method is also briefly mentioned in the last tutorial.

$oHTTP.Open("POST", "", False)

After doing this, you will also need to provide the data to be posted – this data will consist of the fields in the form, as well as their values – the data is stored as a string inside a variable, which is then passed on to the send method.

$sPD = 'name=Jacob&bench=150'

Each form field is separated by an ampersand (&) in the $sPD variable – knowing this, adding additional fields won't be much trouble. Two fields are added in this example, a field called name and one called bench, these fields will be picked up by the script which handles the form submissions.

When you submit a form from your browser, the Content-Type field will be filled automatically if its missing – you need to fill it out manually when using AutoIt. The default value for forms is application/x-www-form-urlencoded. You can use the SetRequestHeader method to set the Content-Type field.

$oHTTP.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")

Brugbart has a php form script at – this should make it easier to learn about http requests. You can try to create a request without the bench field, and see what happens. You may still want to save the response to a text file, as well as catch the response code, explained in the first tutorial.

A Working Example Script

This script will perform a HTTP post request for the URL: – it will catch the response code, and save the body response to a html file for examination.

; The data to be sent
$sPD = 'name=Jacob&bench=150'

; Creating the object
$oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
$oHTTP.Open("POST", "", False)
$oHTTP.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")

; Performing the Request

; Download the body response if any, and get the server status response code.
$oReceived = $oHTTP.ResponseText
$oStatusCode = $oHTTP.Status

If $oStatusCode <> 200 then
 MsgBox(4096, "Response code", $oStatusCode)

; Saves the body response regardless of the Response code 
 $file = FileOpen("Received.html", 2) ; The value of 2 overwrites the file if it already exists
 FileWrite($file, $oReceived)