aqHttp.CreateRequest Method

Applies to TestComplete 15.69, last modified on November 13, 2024

Description

Use the CreateRequest method to create an HTTP request specified by an HTTP verb. 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.CreateRequest(Method, URL, User, Password)

Method [in]    Required    String    
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:

Method

An HTTP verb that defines the operation that the request will use. Must be uppercase (some servers ignore lowercase HTTP verbs).

The following verbs are supported:

  • GET

  • HEAD

  • POST

  • PUT

  • DELETE

  • OPTIONS

  • TRACE

  • PATCH

Tip: For the GET and POST operations, you can also use the CreateGetRequest and CreatePostRequest methods respectively.

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.

  • 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 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
}

See Also

CreateGetRequest Method
CreatePostRequest Method
aqHttp Object

Highlight search results