aqHttp.CreateGetRequest Method

Applies to TestComplete 15.31, last modified on March 17, 2022

Description

Use the CreateGetRequest method to create an HTTP GET request to the specified URL. The method will return an aqHttpRequest object describing the created request.

Note: This method is similar to the Open method of the IWinHttpRequest interface.

Declaration

aqHttp.CreateGetRequest(URL, User, Password)

URL [in]    Required    String    
User [in]    Optional    String    
Password [in]    Optional    String    
Result The aqHttpRequest object

Applies To

The method is applied to the following object:

Parameters

The method has the following parameters:

URL

An absolute URL path to the resource, to which you want to send the request. Must include the protocol.

For example, https://smartbear.com or https://support.smartbear.com.

If the parameter does not include the protocol (for example, www.support.smartbear.com), the Send method of the created aqHttpRequest object will fail.

User

If the Basic authentication is used, the name of the user on whose behalf the request will be sent. See below.

Password

If the Basic authentication is used, the password of the user on whose behalf the request will be sent. See below.

Result Value

The aqHttpRequest object.

Remarks

  • If your target server uses the Basic authentication, pass the user name and password as the method parameters. However, TestComplete will not send the authentication information in the first request. According to the HTTP specification, before sending the authorization header, TestComplete should receive a 401 response (Access Denied). Then TestComplete will send the authentication.

    If your server requires preemptive authentication (that is, it expects the authorization header in each request) or if it responds with an unusual status code (like 400 or 403), TestComplete will not be able to send the request correctly. See the description of the aqHttp object to learn about possible workarounds.

  • The Send method of the aqHttpRequest object that the method returns will send a request without the body content, even if you pass some data as a parameter.

  • If a URL, to which you send the request, includes a query parameter after the host name, put the escape character (/) before it:

    http://example.com/?param=value – Correct;
    http://example.com:8080?param=value – Incorrect (no slash)

    http://example.com:8080/?param=value – Correct;
    http://example.com:8080?param=value – Incorrect (no slash)

    http://example.com:8080/subpath?param=value – Correct (query parameters follow a resource, not the host)
    http://example.com:8080/subpath/?param=value – Also correct

Example

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://httpbin.org";

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

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

  if (aqHttpResponse != null)
  {
    // 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 the response body to a file and place it to the project folder
    aqHttpResponse.SaveToFile(Project.Path + "body.txt");
  }
}

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

  if aqHttpResponse != None:
    # 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 the response body to a file and place it to the project folder
    aqHttpResponse.SaveToFile(Project.Path + "body.txt")

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

  If Not aqHttpResponse Is Nothing Then
    ' 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 the response body to a file and place it to the project folder
    aqHttpResponse.SaveToFile(Project.Path & "body.txt")
  End If
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();

  if aqHttpResponse <> nil then
    begin
    // 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 the response body to a file and place it to the project folder
    aqHttpResponse.SaveToFile(Project.Path + 'body.txt');
  end;
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"]();

  if (aqHttpResponse != null)
  {
    // 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 the response body to a file and place it to the project folder
    aqHttpResponse["SaveToFile"](Project["Path"] + "body.txt");
  }
}

See Also

CreatePostRequest Method
CreateRequest Method
aqHttp Object

Highlight search results