How can i send custom headers with URLRequest

13,221

Here's an implementation that I recently made that worked great for me :

var url:String = _baseURL + "/utils.php";

var headers:Array = [
    new URLRequestHeader("_sessionKey", _sessionKey),
    new URLRequestHeader("_userId", _sessionUserId)
];

var request:URLRequest = new URLRequest();
request.requestHeaders = headers;
request.method = URLRequestMethod.POST;
request.url = url;

/////////////////// 

var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.TEXT; //don't know if this is really needed 
loader.addEventListener(Event.COMPLETE, handleSuccess);
loader.addEventListener(IOErrorEvent.IO_ERROR, handleError);
loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, handleError);

loader.load(request);

The only big difference that I can see is that I am creating a new array with the headers in it, then assigning that to the requestHeaders property of request, instead of creating a new URLRequestHeader and trying to push it directly into request.requestHeaders. Everything else looks pretty good to me.

Hope this helps! And good luck!

Share:
13,221
Erik Sapir
Author by

Erik Sapir

Updated on June 11, 2022

Comments

  • Erik Sapir
    Erik Sapir about 2 years

    I want to add Authorization header to my requests. I added the following line in crossdomain.xml of the server:

    <allow-http-request-headers-from domain="*" headers="Authorization"/>
    

    And still, the header is not sent (checked with Wireshark).

    Am i missing something?

    EDIT:

    the code of the urlRequest:

    var request:URLRequest = new URLRequest();
    request.method = URLRequestMethod.POST;
    request.url = this.uploadURL;
    request.data = post;
    
    var requestHeader:URLRequestHeader = new URLRequestHeader("Authorization", "Basic ZXNhcGlyK2xhQGdtYWlsLmNvbTpFcmlrU2FwaXIyOQ==");                   
    
    
    request.requestHeaders.push(requestHeader);
    
    • Eugeny89
      Eugeny89 about 13 years
      please post a code of UrlRequest creation.
  • BlueRaja - Danny Pflughoeft
    BlueRaja - Danny Pflughoeft over 10 years
    Please be aware that Flash can only send custom headers for POST requests, not GET requests.
  • Tom Teman
    Tom Teman over 9 years
    Another important disclaimer - not all headers can be set (especially - Authorization and Cookie headers): help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/‌​…