Description
Use the aqHttp
object to create HTTP requests. You can then send the created requests by using their Send
method.
Members
Remarks
-
The
aqHttp
object is based on theIWinHTTPRequest
interface by Microsoft. Many of their methods and properties are similar. -
In Python scripts, you can also create HTTP requests with the internal urllib or third-party requests package.
-
Currently, if your test computer connects to the Internet through the proxy server that requires you to specify the proxy address and port in the Internet Explorer settings (that is, uses the "transparent" method of authentication), you will not be able to send requests created with the
aqHttp
object methods. -
The
META
tag is not supported. For instance, if the charset encoding of the response returned from your tested web server is set by theMETA
tag, it will not be recognized, and TestComplete may convert and show the response body content incorrectly.
Example
Send a GET Request
The example below demonstrates how you can use the aqHttp
object to create an HTTP GET request and send it to get a response from the server. To work with the created request, the code uses the aqHttpRequest
object. To work with the server response, the sample code uses the aqHttpResponse
object:
JavaScript, JScript
function httpGetRequest()
{
var address = "http://now.httpbin.org";
// Create an aqHttpRequest object
var aqHttpRequest = aqHttp.CreateGetRequest(address);
// Send the request, get an aqHttpResponse object
var aqHttpResponse = aqHttpRequest.Send();
// Read the response data
Log.Message(aqHttpResponse.AllHeaders); // All headers
Log.Message(aqHttpResponse.GetHeader("Content-Type")); // A specific header
Log.Message(aqHttpResponse.StatusCode); // A status code
Log.Message(aqHttpResponse.StatusText); // A status text
Log.Message(aqHttpResponse.Text); // A response body
// Save a response body to a file
aqHttpResponse.SaveToFile("C:\\Work\\body.txt");
}
Python
def httpGetRequest():
address = "http://now.httpbin.org"
# Create an aqHttpRequest object
aqHttpRequest = aqHttp.CreateGetRequest(address)
# Send the request, get an aqHttpResponse object
aqHttpResponse = aqHttpRequest.Send()
# Read the response data
Log.Message(aqHttpResponse.AllHeaders) # All headers
Log.Message(aqHttpResponse.GetHeader("Content-Type")) # A specific header
Log.Message(aqHttpResponse.StatusCode) # A status code
Log.Message(aqHttpResponse.StatusText) # A status text
Log.Message(aqHttpResponse.Text) # A response body
# Save a response body to a file
aqHttpResponse.SaveToFile("C:\\Work\\body.txt")
VBScript
Sub httpGetRequest
Dim address, aqHttpRequest, aqHttpResponse
address = "http://now.httpbin.org"
' Create an aqHttpRequest object
Set aqHttpRequest = aqHttp.CreateGetRequest(address)
' Send the request, get an aqHttpResponse object
Set aqHttpResponse = aqHttpRequest.Send()
' Read the response data
Log.Message(aqHttpResponse.AllHeaders) ' All headers
Log.Message(aqHttpResponse.GetHeader("Content-Type")) ' A specific header
Log.Message(aqHttpResponse.StatusCode) ' A status code
Log.Message(aqHttpResponse.StatusText) ' A status text
Log.Message(aqHttpResponse.Text) ' A response body
' Save a response body to a file
aqHttpResponse.SaveToFile("C:\\Work\\body.txt")
End Sub
DelphiScript
function httpGetRequest;
var address, aqHttpRequest, aqHttpResponse;
begin
address := 'http://now.httpbin.org';
// Create an aqHttpRequest object
aqHttpRequest := aqHttp.CreateGetRequest(address);
// Send the request, get an aqHttpResponse object
aqHttpResponse := aqHttpRequest.Send();
// Read the response data
Log.Message(aqHttpResponse.AllHeaders); // All headers
Log.Message(aqHttpResponse.GetHeader('Content-Type')); // A specific header
Log.Message(aqHttpResponse.StatusCode); // A status code
Log.Message(aqHttpResponse.StatusText); // A status text
Log.Message(aqHttpResponse.Text); // A response body
// Save a response body to a file
aqHttpResponse.SaveToFile('C:\\Work\\body.txt');
end;
C++Script, C#Script
function httpGetRequest()
{
var address = "http://now.httpbin.org";
// Create an aqHttpRequest object
var aqHttpRequest = aqHttp["CreateGetRequest"](address);
// Send the request, get an aqHttpResponse object
var aqHttpResponse = aqHttpRequest["Send"]();
// Read the response data
Log["Message"](aqHttpResponse["AllHeaders"]); // All headers
Log["Message"](aqHttpResponse["GetHeader"]("Content-Type")); // A specific header
Log["Message"](aqHttpResponse["StatusCode"]); // A status code
Log["Message"](aqHttpResponse["StatusText"]); // A status text
Log["Message"](aqHttpResponse["Text"]); // A response body
// Save a response body to a file
aqHttpResponse["SaveToFile"]("C:\\Work\\body.txt");
}
Send a POST Request
The example below demonstrates how you can use the aqHttp
object to create an HTTP POST request that sends user authentication information to a server.
JavaScript, JScript
function httpPostRequest()
{
var address = "http://petstore.swagger.io/v2/pet";
var username = "Smith, John";
var password = "P@55W0RD";
// Define the request body JSON string
var requestBody = '{ "id": 23, "category": { "id": 5, "name": "dogs"}, "name": "fido"}';
// Create the aqHttpRequest object
var aqHttpRequest = aqHttp.CreatePostRequest(address, username, password);
// Specify the Content-Type header value
aqHttpRequest.SetHeader("Content-Type", "application/json");
// Send the request, create the aqHttpResponse object
var aqHttpResponse = aqHttpRequest.Send(requestBody)
// Check the response:
Log.Message(aqHttpResponse.StatusCode); // A status code
Log.Message(aqHttpResponse.Text); // A body
}
Python
def httpPostRequest():
address = "http://petstore.swagger.io/v2/pet"
username = "Smith, John"
password = "P@55W0RD"
# Define the request body JSON string
requestBody = '{ "id": 23, "category": { "id": 5, "name": "dogs"}, "name": "fido"}'
# Create the aqHttpRequest object
aqHttpRequest = aqHttp.CreatePostRequest(address, username, password)
# Specify the Content-Type header value
aqHttpRequest.SetHeader("Content-Type", "application/json")
# Send the request, create the aqHttpResponse object
aqHttpResponse = aqHttpRequest.Send(requestBody)
# Check the response:
Log.Message(aqHttpResponse.StatusCode) # A status code
Log.Message(aqHttpResponse.Text) # A body
VBScript
Sub httpPostRequest
Dim address, username, password, requestbody, aqHttpRequest, aqHttpResponse
address = "http://petstore.swagger.io/v2/pet"
username = "Smith, John"
password = "P@55W0RD"
' Define the request body JSON string
requestBody = "{ ""id"": 23, ""category"": { ""id"": 5, ""name"": ""dogs""}, ""name"": ""fido""}"
' Create the aqHttpRequest object
Set aqHttpRequest = aqHttp.CreatePostRequest(address, username, password)
' Specify the Content-Type header value
Call aqHttpRequest.SetHeader("Content-Type", "application/json")
' Send the request, create the aqHttpResponse object
Set aqHttpResponse = aqHttpRequest.Send(requestBody)
' Check the response:
Log.Message(aqHttpResponse.StatusCode) ' A status code
Log.Message(aqHttpResponse.Text) ' A body
End Sub
DelphiScript
function httpPostRequest;
var address, username, password, requestBody, aqHttpRequest, aqHttpResponse;
begin
address := 'http://petstore.swagger.io/v2/pet';
username := 'Smith, John';
password := 'P@55W0RD';
// Define the request body JSON string
requestBody := '{ "id": 23, "category": { "id": 5, "name": "dogs"}, "name": "fido"}';
// Create the aqHttpRequest object
aqHttpRequest := aqHttp.CreatePostRequest(address, username, password);
// Specify the Content-Type header value
aqHttpRequest.SetHeader('Content-Type', 'application/json');
// Send the request, create the aqHttpResponse object
aqHttpResponse := aqHttpRequest.Send(requestBody);
// Check the response:
Log.Message(aqHttpResponse.StatusCode); // A status code
Log.Message(aqHttpResponse.Text); // A body
end;
C++Script, C#Script
function httpPostRequest()
{
var address = "http://petstore.swagger.io/v2/pet";
var username = "Smith, John";
var password = "P@55W0RD";
// Define the request body JSON string
requestBody = '{ "id": 23, "category": { "id": 5, "name": "dogs"}, "name": "fido"}';
// Create the aqHttpRequest object
var aqHttpRequest = aqHttp["CreatePostRequest"](address, username, password);
// Specify the Content-Type header value
aqHttpRequest["SetHeader"]("Content-Type", "application/json");
// Send the request, create the aqHttpResponse object
var aqHttpResponse = aqHttpRequest["Send"](requestBody);
// Check the response:
Log["Message"](aqHttpResponse["StatusCode"]); // A status code
Log["Message"](aqHttpResponse["Text"]); // A body
}
Send a PUT Request
The example below demonstrates how you can use the aqHttp
object to create a HTTP PUT request that contains information in the XML format and send the request to a server.
JavaScript, JScript
function httpRequest()
{
var address = "http://petstore.swagger.io/v2/pet";
// Specify the request body XML string
var requestBody = "<Pet><id>23</id><name>Rex</name></Pet>";
// You can also read the needed information from a file:
// var requestBody = aqFile.ReadWholeTextFile("C:\\Work\\pet.xml", aqFile.ctANSI)
// Create the aqHttpRequest object
var aqHttpRequest = aqHttp.CreateRequest("PUT", address);
// Specify the Content-Type header value
aqHttpRequest.SetHeader("Content-Type", "application/xml");
// Send the request, create the aqHttpResponse object
var aqHttpResponse = aqHttpRequest.Send(requestBody);
// Check the response:
Log.Message(aqHttpResponse.StatusCode); // A status code
Log.Message(aqHttpResponse.Text); // A body
}
Python
def httpRequest():
address = "http://petstore.swagger.io/v2/pet"
# Specify the request body XML string
requestBody = "<Pet><id>23</id><name>Rex</name></Pet>"
# You can also read the needed information from a file:
# requestBody = aqFile.ReadWholeTextFile("C:\\Work\\pet.xml", aqFile.ctANSI)
# Create the aqHttpRequest object
aqHttpRequest = aqHttp.CreateRequest("PUT", address)
# Specify the Content-Type header value
aqHttpRequest.SetHeader("Content-Type", "application/xml")
# Send the request, create the aqHttpResponse object
aqHttpResponse = aqHttpRequest.Send(requestBody)
# Check the response:
Log.Message(aqHttpResponse.StatusCode) # A status code
Log.Message(aqHttpResponse.Text) # A body
VBScript
Sub httpRequest
Dim address, requestBody, aqHttpRequest, aqHttpResponse
address = "http://petstore.swagger.io/v2/pet"
' Specify the request body XML string
requestBody = "<Pet><id>23</id><name>Rex</name></Pet>"
' You can also read the needed information from a file:
' requestBody = aqFile.ReadWholeTextFile("C:\\Work\\pet.xml", aqFile.ctANSI)
' Create the aqHttpRequest object
Set aqHttpRequest = aqHttp.CreateRequest("PUT", address)
' Specify the Content-Type header value
Call aqHttpRequest.SetHeader("Content-Type", "application/xml")
' Send the request, create the aqHttpResponse object
Set aqHttpResponse = aqHttpRequest.Send(requestBody)
' Check the response:
Log.Message(aqHttpResponse.StatusCode) ' A status code
Log.Message(aqHttpResponse.Text) ' A body
End Sub
DelphiScript
function httpRequest;
var address, requestBody, aqHttpRequest, aqHttpResponse;
begin
address := 'http://petstore.swagger.io/v2/pet';
// Specify the request body XML string
requestBody := '<Pet><id>23</id><name>Rex</name></Pet>';
// You can also read the needed information from a file:
// requestBody := aqFile.ReadWholeTextFile('C:\\Work\\pet.xml', aqFile.ctANSI)
// Create the aqHttpRequest object
aqHttpRequest := aqHttp.CreateRequest('PUT', address);
// Specify the Content-Type header value
aqHttpRequest.SetHeader('Content-Type', 'application/xml');
// Send the request, create the aqHttpResponse object
aqHttpResponse := aqHttpRequest.Send(requestBody);
// Check the response:
Log.Message(aqHttpResponse.StatusCode); // A status code
Log.Message(aqHttpResponse.Text); // A body
end;
C++Script, C#Script
function httpRequest()
{
var address = "http://petstore.swagger.io/v2/pet";
// Specify the XML request body XML string
var requestBody = "<Pet><id>23</id><name>Rex</name></Pet>";
// You can also read the needed information from a file:
// var requestBody = aqFile["ReadWholeTextFile"]("C:\\Work\\pet.xml", aqFile.ctANSI)
// Create the aqHttpRequest object
var aqHttpRequest = aqHttp["CreateRequest"]("PUT", address);
// Specify the Content-Type header value
aqHttpRequest["SetHeader"]("Content-Type", "application/xml");
// Send the request, create the aqHttpResponse object
var aqHttpResponse = aqHttpRequest["Send"](requestBody);
// Check the response:
Log["Message"](aqHttpResponse["StatusCode"]); // A status code
Log["Message"](aqHttpResponse["Text"]); // A body
}
Send a Request to a Server That Uses Preemptive Authentication
If your server requires preemptive authentication, you can use the SetHeader method to force the authorization header to appear in each request. Keep in mind that the user name and password values must be base64-encoded.
JavaScript, JScript
function httpAuthRequest()
{
var address = "http://petstore.swagger.io/v2/store/inventory";
var username = "Smith, John";
var password = "P@55W0RD";
// Convert the user credentials to base64 for preemptive authentication
var credentials = aqConvert.VarToStr(dotNET.System.Convert.ToBase64String
(dotNET.System_Text.Encoding.UTF8.GetBytes_2(username + ":" + password)));
var aqHttpRequest = aqHttp.CreateGetRequest(address);
// Send the Authorization header with a request
aqHttpRequest.SetHeader("Authorization", "Basic " + credentials);
var aqHttpResponse = aqHttpRequest.Send();
// Log the status code of the response
Log.Message(aqHttpResponse.StatusCode);
// Log the text of the response
Log.Message(aqHttpResponse.Text);
}
Python
import base64
def httpAuthRequest():
address = "http://petstore.swagger.io/v2/store/inventory"
username = "Smith, John"
password = "P@55W0RD"
# Convert the user credentials to base64 for preemptive authentication
credentials = bytes(username + ":" + password, 'utf-8') # Convert a string to a bytes object
# Simple base64 encoding
credentials = base64.b64encode(credentials, altchars=None)
# Decode the bytes object to a string to use it in the header
credentials = credentials.decode("utf-8")
aqHttpRequest = aqHttp.CreateGetRequest(address)
# Send the Authorization header with a request
aqHttpRequest.SetHeader("Authorization", "Basic " + credentials)
aqHttpResponse = aqHttpRequest.Send()
# Log the status code of the response
Log.Message(aqHttpResponse.StatusCode)
# Log the text of the response Log.Message(aqHttpResponse.Text)
VBScript
Sub httpAuthRequest
Dim address, requestBody, username, password, aqHttpRequest, aqHttpResponse
address = "http://petstore.swagger.io/v2/store/inventory"
username = "Smith, John"
password = "P@55W0RD"
' Convert the user credentials to base64 for preemptive authentication
credentials = aqConvert.VarToStr(dotNET.System.Convert.ToBase64String _
(dotNET.System_Text.Encoding.UTF8.GetBytes_2(username + ":" + password)))
Set aqHttpRequest = aqHttp.CreateGetRequest(address)
' Send the Authorization header with a request
Call aqHttpRequest.SetHeader("Authorization", "Basic " + credentials)
Set aqHttpResponse = aqHttpRequest.Send()
' Log the status code of the response
Log.Message(aqHttpResponse.StatusCode)
' Log the text of the response
Log.Message(aqHttpResponse.Text)
End Sub
DelphiScript
function httpAuthRequest;
var address, username, password, credentials, aqHttpRequest, aqHttpResponse;
begin
address := 'http://petstore.swagger.io/v2/store/inventory';
username := 'Smith, John';
password := 'P@55W0RD';
// Convert the user credentials to base64 for preemptive authentication
credentials := aqConvert.VarToStr(dotNET.System.Convert.ToBase64String
(dotNET.System_Text.Encoding.UTF8.GetBytes_2(username + ':' + password)));
aqHttpRequest := aqHttp.CreateGetRequest(address);
// Send the Authorization header with a request
aqHttpRequest.SetHeader('Authorization', 'Basic ' + credentials);
aqHttpResponse := aqHttpRequest.Send();
// Log the status code of the response
Log.Message(aqHttpResponse.StatusCode);
// Log the text of the response
Log.Message(aqHttpResponse.Text);
end;
C++Script, C#Script
function httpAuthRequest()
{
var address = "http://petstore.swagger.io/v2/store/inventory";
var username = "Smith, John";
var password = "P@55W0RD";
// Convert the user credentials to base64 for preemptive authentication
var credentials = aqConvert["VarToStr"](dotNET["System"]["Convert"]["ToBase64String"]
(dotNET["System_Text"]["Encoding"]["UTF8"]["GetBytes_2"](username + ":" + password)));
var aqHttpRequest = aqHttp["CreateGetRequest"](address);
// Send the Authorization header with a request
aqHttpRequest["SetHeader"]("Authorization", "Basic " + credentials);
var aqHttpResponse = aqHttpRequest["Send"]();
// Log the status code of the response Log["Message"](aqHttpResponse["StatusCode"]);
// Log the text of the response
Log["Message"](aqHttpResponse["Text"]);
}
See Also
aqHttpRequest Object
aqHttpResponse Object
aqHttpStatusCode Object