JavaScript global method: doHTTPRequest

Issues an HTTP request to a specified URL.

Syntax

doHTTPRequest( HTTP command, URL, Headers, POST body, Connect Timeout, Read Timeout, Obsolete, Response headers, Binary request data, Binary response data, Follow redirects);

Arguments

The following arguments are valid for this function:

Argument Data type Required Description
HTTP command String Yes This argument specifies a command verb. We support GET, POST, PUT, HEAD, and DELETE.
URL String Yes This argument specifies the URL to which the script should send an HTTP request. If the HTTP request requires SSL then the URL must start with https.
Headers Array Yes This argument contains a JavaScript array of Service Manager-defined Header objects or an empty array. See JavaScript object: Header for more information.
POST body String No This argument contains the body of a POST command request.
Connect Timeout Number No

This argument specifies the number of seconds an HTTP request has to establish a connection before it times out.

Note The default value is 120 seconds, and the maximum value is 600 seconds. That is, if you try to set the value to a number larger than 600 seconds, the actual timeout period will be 120 seconds. In addition, setting the value to 0 also makes the actual timeout period 120 seconds.

Read Timeout Number No

This argument specifies the number of seconds an HTTP request has to read data before it times out.

Note The default value is 120 seconds, and the maximum value is 600 seconds. That is, if you try to set the value to a number larger than 600 seconds, the actual timeout period will be 120 seconds. In addition, setting the value to 0 also makes the actual timeout period 120 seconds.

Receive timeout (Obsolete) Number No This argument is obsolete.
Response headers Object No This argument receives the headers of an HTTP response.
Binary request data Boolean No This argument specifies if the content of a post body is binary data.
Binary response data Boolean No This argument specifies if the content of an HTTP response is binary data.
Follow redirects Boolean No This argument specifies if the HTTP client automatically follows the HTTP redirects (requests with the response code 3xx). The default value is true.

Return values

A string containing an HTTP response or an error message.

Description

This method sends an HTTP request to a remote server.

Note If the charset is not defined in the “Content-Type” header in the parameter “Headers”, the charset for the message body will be UTF-8 by default.

Example

This example does the following:

  • Sends an HTTP request to the Service Manager Web services API
  • If successful, writes the response to a local file
  • If unsuccessful, displays the HTTP error message

Note To see a working example of the doHTTPRequest method that incorporates multiple error checking conditions, open the SOAP script in the script library. The WSDL2js function uses the doHTTPRequest method at line 114 as part of a try/catch statement.

This example requires the following sample data:

  • The URL to the Service Manager Web services API

    // Use the following url value to produce an HTTP 400 error.
    //var url = "http://localhost:13080/IncidentManagement.wsdl";
    
    // Use the following url value to produce valid WSDL.
    var url = "http://localhost:13080/SM/7/IncidentManagement.wsdl";
    
    var headers = new Array();
    var WSDLrequest;
    var reply;
    var respHeaders = new Object();
    try
    	{
    	 WSDLrequest = doHTTPRequest( "GET", url, headers, null, 10, 10, null, respHeaders );
    	 print( "The response headers of the doHTTPRequest is:" );
    	 for( var name in respHeaders )
    	{
    	    print( name + "=" + respHeaders[name] );
    	}
    	  print( "The result of the doHTTPRequest is \n" + WSDLrequest );
    	  reply = writeFile( "C:\\IncidentManagement.wsdl", "text", WSDLrequest );
    	}
    catch ( e )
    	{
    	 print( "WSDL request failed with exception \n" + e );
    	}