setRequestHeader Method (IXMLHTTPRequest)


Specifies the name of an HTTP header.

JScript Syntax

oXMLHttpRequest.setRequestHeader(bstrHeader, bstrValue);  


A string. A header name to set; for example, "depth". This parameter should not contain a colon and should be the actual text of the HTTP header.

A string. The value of the header; for example, "infinity".


You must call the open method before you call this method. Otherwise, an error will occur.


The following VBScript example posts a DOMDocument to an Active Server Page (ASP) on a server and returns the result as a new XML document.


Function onLoad()  
   Dim mydata, pi  
   Set mydata = CreateObject("Msxml2.DOMDocument.6.0")  
   Set pi = mydata.createProcessingInstruction("xml", "version='1.0' encoding='UTF-8'")  
   mydata.insertBefore pi, mydata.firstChild  
End function  
Function sendInfo()  
   Dim MyHttp  
   'Do validation of input data before sending it.  
   If(Not(customerName.value = ""))   then  
      With MyData.documentElement  
         .getElementsByTagName("Name").item(0).text = customerName.value  
         .getElementsByTagName("Phone").item(0).text = customerPhoneNum.value  
      End With  
      Set MyHttp=CreateObject("Msxml2.XMLHTTP.6.0") "POST", "http://localhost/httpreqserver.asp", False  
      'Simulate message sent by a custom user agent.  
      MyHttp.setRequestHeader "User-Agent", "MyCustomUser"  
      MyData.async = False  
      MyHttp.send mydata.XMLDocument  
      Document.Write MyHttp.responseText  
      Document.Write "Invalid data."  
   End If  
End function  
<BODY LANGUAGE="JScript" ONLOAD="Return onLoad()">  
<TABLE BORDER="2" ALIGN="center">  
<TR><TD WIDTH="150" ALIGN="center">  
   <INPUT NAME="customerName" TYPE="EDIT"/>  
<TR><TD WIDTH="150" align="CENTER">  
   <LABEL>Telephone number</LABEL>  
   <INPUT NAME="customerPhoneNum" type="EDIT"/>  

   <TR><TD WIDTH="150" ALIGN="CENTER">  
      <INPUT TYPE="BUTTON" VALUE="Send Information" ALIGN="CENTER" ONCLICK="sendInfo()"/>  

<XML id="MyData">  

ASP File (httpreqserver.asp)

   Response.Expires = -1000;  
   // Load the posted XML document.  
   var doc = Server.CreateObject("Msxml2.DOMDocument.6.0");  
   var result = Server.CreateObject("Msxml2.DOMDocument.6.0");  
   // Now process the order and build the result document.  
   var userAgent = Request.ServerVariables("HTTP_User-Agent");  
   var OutputString="Data for "+  
         doc.documentElement.childNodes.item(0).text +  
         " (" + doc.documentElement.childNodes.item(1).text +  
         ") added";  
   Response.ContentType = "text/xml";  
   if(userAgent == "MyCustomUser")  
      result.loadXML("<result>" + OutputString +" </result>");  
      var pi = result.createProcessingInstruction("xml", "version='1.0'");  
      result.insertBefore( pi, result.firstChild);;        
      Response.Write("<P><B>" + OutputString+" </B></P>");  

Try It!

To run this sample, you need access to a computer running Internet Information Services (IIS) 5.0 or later.

  1. Copy the HTML code provided above, and paste it into Notepad.

  2. Save the file a form.htm to a valid Web virtual directory, such as C:\Inetpub\wwwroot, on your Web server computer.


    If you are not running IIS locally on your computer, locate the following line:

    Note "POST", "http://localhost/httpreqserver.asp", False


    Substitute the name of the remote computer for the characters "localhost", and save the file.

  3. Copy the ASP code provided above, and paste it into Notepad.

  4. Save the file as httpreqserver.asp, in the same Web virtual directory you used in Step 1.

  5. Open your browser to the Web URL location where you saved the sample HTML file, such as http://localhost/form.htm.

  6. Enter the name and phone number information, and click Send Information to add/submit the XML to the ASP request server page.


When run, the sample Web application should return as output in the browser the name and phone number you entered as input. To verify that this information was generated using the XML <result> node string from httpreqserver.asp, you can do the following:

  1. From within Internet Explorer, click View, and then click Source.

  2. You can observe that the source matches the string that was generated by the ASP request server page in this format:

    <?xml version="1.0"?>
    <result>Data for [name input] ([phone number input] )added.</result>

C/C++ Syntax

HRESULT setRequestHeader(BSTR bstrHeader, BSTR bstrValue);  


bstrHeader [in]
A header name to set; for example, "depth". This parameter should not contain a colon and should be the actual text of the HTTP header.

The value of the header; for example, "infinity".

Return Values

The value returned if successful.


If another header already exists with this name, it is concatenated to the previous header.


Implemented in: MSXML 3.0 and MSXML 6.0

See Also

getResponseHeader Method (IXMLHTTPRequest)