Sending and Receiving HTTP Requests From Script Tests

Applies to TestComplete 14.20, last modified on September 11, 2019

This topic explains how to send different types of HTTP requests, obtain responses from the target server, and then use the responses data in your script tests.

Sending HTTP GET Requests

To create an HTTP GET request, you can use the CreateGetRequest method of the aqHttp object. To send the request and obtain its response from the server, use the Send method of the aqHttpRequest object:

JavaScript, JScript

function httpGetRequest()
{
  var address = "http://httpbin.org";

  // Create an aqHttpRequest object
  var aqHttpRequest = aqHttp.CreateGetRequest(address);

  // Send the request, get an aqHttpResponse object
  var aqHttpResponse = aqHttpRequest.Send();
}

Python

def httpGetRequest(): 
  address = "http://httpbin.org"

  # Create an aqHttpRequest object
  aqHttpRequest = aqHttp.CreateGetRequest(address)

  # Send the request, get an aqHttpResponse object
  aqHttpResponse = aqHttpRequest.Send()

VBScript

Sub httpGetRequest
  Dim address, aqHttpRequest, aqHttpResponse
  address = "http://httpbin.org"

  ' Create an aqHttpRequest object
  Set aqHttpRequest = aqHttp.CreateGetRequest(address)

  ' Send the request, get an aqHttpResponse object
  Set aqHttpResponse = aqHttpRequest.Send()
End Sub

DelphiScript

function httpGetRequest;
var address, aqHttpRequest, aqHttpResponse;
begin
  
  address := 'http://httpbin.org';

  // Create an aqHttpRequest object
  aqHttpRequest := aqHttp.CreateGetRequest(address);

  // Send the request, get an aqHttpResponse object
  aqHttpResponse := aqHttpRequest.Send();
  
end;

C++Script, C#Script

function httpGetRequest()
{
  var address = "http://httpbin.org";

  // Create an aqHttpRequest object
  var aqHttpRequest = aqHttp["CreateGetRequest"](address);

  // Send the request, get an aqHttpResponse object
  var aqHttpResponse = aqHttpRequest["Send"]();
}

Note: You can also use the CreateRequest method for creating GET requests.

Sending HTTP POST Requests

To create an HTTP POST request, you can use the CreatePostRequest method of the aqHttp object. To send the request and obtain its response from the server, use the Send method of the aqHttpRequest object.

The example below demonstrates how you can 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)
}

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)

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)
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);
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);
}

Note: You can also use the CreateRequest method for creating POST requests.

Sending Other Types of Requests

To create one of the available requests other than GET and POST, use the CreateRequest method, in which you can specify a request type as a parameter.

The example below demonstrates how you can use the CreateRequest method of the aqHttp object to create an 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);
}

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)

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)
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);
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);
}

Sending Requests to a Server With 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();
}

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()

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()
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();
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"]();
}

Using Timeouts

A test engine might fail to send or receive requests for various reasons. For example, a connection with a target server might be temporarily lost, a request cannot be sent or a response is not received yet. To solve such problems, specify timeout values for sending HTTP requests and receiving responses from the target server by using the SetTimeouts method.

JavaScript, JScript

function httpRequest()
{
  var address = "https://downloads.smartbear.com/TestComplete1410_Samples.exe";

  // Create an aqHttpRequest object
  var aqHttpRequest = aqHttp.CreateGetRequest(address);
    
  // Specify timeout values (Resolve, Connect, Send, Receive)
  aqHttpRequest.SetTimeouts(30000,60000,30000,70000);
  
  // Send the request, get an aqHttpResponse object
  var aqHttpResponse = aqHttpRequest.Send();
  
  // Save a response body to a file
  aqHttpResponse.SaveToFile("C:\\Work\\TestComplete1410_Samples.exe");
}

Python

def httpRequest():
  address = "https://downloads.smartbear.com/TestComplete1410_Samples.exe"

  # Create an aqHttpRequest object
  aqHttpRequest = aqHttp.CreateGetRequest(address)
  
  # Specify timeout values (Resolve, Connect, Send, Receive)
  aqHttpRequest.SetTimeouts(30000,60000,30000,70000)

  # Send the request, get an aqHttpResponse object
  aqHttpResponse = aqHttpRequest.Send()
  
  # Save a response body to a file
  aqHttpResponse.SaveToFile("C:\\Work\\TestComplete1410_Samples.exe")

VBScript

Sub httpRequest
  Dim address, aqHttpRequest, aqHttpResponse
  address = "https://downloads.smartbear.com/TestComplete1410_Samples.exe"

  ' Create an aqHttpRequest object
  Set aqHttpRequest = aqHttp.CreateGetRequest(address)
  
  ' Specify timeout values (Resolve, Connect, Send, Receive)
  Call aqHttpRequest.SetTimeouts(30000,60000,30000,70000)

  ' Send the request, get an aqHttpResponse object
  Set aqHttpResponse = aqHttpRequest.Send()
 
  ' Save a response body to a file
  aqHttpResponse.SaveToFile("C:\\Work\\TestComplete1410_Samples.exe")
End Sub

DelphiScript

function httpRequest;
var address, aqHttpRequest, aqHttpResponse;
begin
  
  address := 'https://downloads.smartbear.com/TestComplete1410_Samples.exe';

  // Create an aqHttpRequest object
  aqHttpRequest := aqHttp.CreateGetRequest(address);
  
  // Specify timeout values (Resolve, Connect, Send, Receive)
  aqHttpRequest.SetTimeouts(30000,60000,30000,70000);

  // Send the request, get an aqHttpResponse object
  aqHttpResponse := aqHttpRequest.Send();
  
  // Save a response body to a file
  aqHttpResponse.SaveToFile('C:\\Work\\TestComplete1410_Samples.exe');
  
end;

C++Script, C#Script

function httpRequest()
{
  var address = "https://downloads.smartbear.com/TestComplete1410_Samples.exe";

  // Create an aqHttpRequest object
  var aqHttpRequest = aqHttp["CreateGetRequest"](address);
  
  // Specify timeout values (Resolve, Connect, Send, Receive)
  aqHttpRequest["SetTimeouts"](30000,60000,30000,70000);


  // Send the request, get an aqHttpResponse object
  var aqHttpResponse = aqHttpRequest["Send"]();
   
  // Save a response body to a file
  aqHttpResponse["SaveToFile"]("C:\\Work\\TestComplete1410_Samples.exe");
}

Working With Responses

After you sent a request and obtained the aqHttpResponse object, you can use it in your script tests and do the following actions with the response data:

  • To get all headers (with their values) of an HTTP response returned by a sent request, use the AllHeaders property.

  • To get a value of a specific header, use the GetHeader method.

  • To get a status code of an HTTP response returned by a sent request, use the StatusCode property.

    Most common status codes

  • To get a status line of an HTTP response returned by a sent request, use the Status Text property.

  • To get the body of an HTTP response returned by a sent request, use the Text property. Note that the obtained text will be presented as a string.

  • To save a response body to a file, use the SaveToFile method. This way, images and files will be parsed from the response.

JavaScript, JScript

function httpResponse()
{
  // Create an aqHttpRequest object
  // Send the request, get an aqHttpResponse object
  
  // 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 httpResponse(): 
  
  # Create an aqHttpRequest object
  # Send the request, get an aqHttpResponse object
  
  # 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 httpResponse
  
  ' Create an aqHttpRequest object
  ' Send the request, get an aqHttpResponse object
  
  ' 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 httpResponse;
var address, aqHttpRequest, aqHttpResponse;
begin
  
  // Create an aqHttpRequest object
  // Send the request, get an aqHttpResponse object
  
  // 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 httpResponse()
{
  // Create an aqRequest object
  // Send the request, get an aqHttpResponse object
  
  // 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");
}

See Also

aqHttpRequest.Send Method
Sending and Receiving HTTP Requests From Keyword Tests

Highlight search results