aqHttpRequest.SetHeader Method

Applies to TestComplete 15.68, last modified on October 16, 2024

Description

Use the SetHeader method to add a header to the aqHttpRequest object.

Note: This method is identical to the SetRequestHeader method of the IWinHttpRequest interface.

Declaration

aqHttpRequest.SetHeader(Header, Value)

Header [in]    Required    String    
Value [in]    Required    String    
Result None

Applies To

The method is applied to the following object:

Parameters

The method has the following parameters:

Header

A string containing the actual name of the HTTP header you want to add. Must not contain colons.

Value

The header value.

Result Value

None.

Remarks

  • To use this method, you need to create the aqHttpRequest object using one of the aqHttp object methods.

  • You can use this method to work with standard headers (such as Content-Type, Accept, and so on) as well as custom headers. Keep in mind that, in most cases, header names and values are case-insensitive, but this may vary depending on the settings of the server you work with.

  • Please note that if you try to use this method to specify a value of the Content-Type header twice within one script, it causes unexpected errors such as The handle is in the wrong state for the requested operation and Cannot create a file when that file already exists. This happens due to the restrictions in the Windows code used to implement the SetHeader method in TestComplete.

  • If a header with the name you specified already exists, its value will not be replaced.

  • To delete a header using this method, specify null as the header's value (that is, null in JavaScript, JScript, C#Script and C++Script, None in Python, Nothing in VBScript, or nil in DelphiScript).

Example

The example below demonstrates how you can use the SetHeader method to define the content type of a request before sending it to the 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
}

See Also

Send Method
SetTimeouts Method
aqHttpRequest Object

Highlight search results