Description
Use the Send
method to send an HTTP request to a web server.
Before sending the request, create the aqHttpRequest
object with the methods of the aqHTTP
object.
Declaration
aqHttpRequest.Send(Body)
Body | [in] | Optional | Variant | |
Result | The aqHttpResponse object |
Applies To
The method is applied to the following object:
Parameters
The method has the following parameter:
Body
The data you want to send to the server as the request body (for example, a string containing JSON data).
-
To send JSON or XML data, create a new
Content-Type
header with theapplication/json
orapplication/xml
value respectively. To do that, use theSetHeader
method. -
If you need to send files (for example, images), convert them to bytes first. To do that, use the
ReadBytes
method of theaqBinaryFile
object.
Result Value
The aqHttpResponse
object.
Remarks
-
This method is an analog of the
Send
method of theIWinHttpRequest
interface. -
If you have created the
aqHttpRequest
object using theCreateGetRequest
method of theaqHttp
object, this method will send the request without thebody
, even if theBody
parameter is not empty. -
You can process values by using the internal means of the language you use to work with JSON data for your requests. In Python, the json library is used for that purpose. In JavaScript, use
JSON.stringify(value)
to convert JavaScript objects to JSON strings. -
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
The example below demonstrates how you can use the Send
method to send a POST request with JSON content 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
}