aqHttp.CreatePostRequest Method

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

Description

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

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

Declaration

aqHttp.CreatePostRequest(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.

  • 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 POST request for user authentication on a server and post the information you need in the JSON format.

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
}

See Also

CreateGetRequest Method
CreateRequest Method
aqHttp Object

Highlight search results