Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i send custom headers with URLRequest

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);
like image 678
Erik Sapir Avatar asked Jun 01 '11 09:06

Erik Sapir


People also ask

How do I send a custom request header?

For example, to send a GET request with a custom header name, you can use the "X-Real-IP" header, which defines the client's IP address. For a load balancer service, "client" is the last remote host. Your load balancer intercepts traffic between the client and your server.

How do I send a header from URL?

Fill out the Create a header fields as follows: In the Name field, enter the name of your header rule (for example, My header ). From the Type menu, select Request, and from the Action menu, select Set. In the Destination field, enter the name of the header affected by the selected action.

Can I add custom header to HTTP request?

In the Home pane, double-click HTTP Response Headers. In the HTTP Response Headers pane, click Add... in the Actions pane. In the Add Custom HTTP Response Header dialog box, set the name and value for your custom header, and then click OK.

Can we send header in GET request?

Yes, you can send any HTTP headers with your GET request. For example, you can send user authentication data in the Authorization header, send browser cookies in the Cookie header, or even send some additional details about your request in custom headers like X-Powered-By or X-User-IP.


1 Answers

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!

like image 66
Ian Avatar answered Oct 31 '22 05:10

Ian