Can't set "Content-Type" for URLRequest in as3

Andrei picture Andrei · Sep 10, 2011 · Viewed 8.6k times · Source

I'm trying to set Content-Type for URLRequest headers to "application/json". Here is the code:

var request:URLRequest = new URLRequest("http://localhost");
request.contentType =  "application/json; charset=UTF-8";

But as you can see on screenshot Content-Type is not in headers:

http://screencast.com/t/vHxHbSUOFM

But it's in request body:

http://screencast.com/t/irB16taO

How to make it right?

Answer

Dave Mackintosh picture Dave Mackintosh · Sep 11, 2011

You need to use the URLRequestHeader class to set headers and push it. See below for a simple example,

var hdr:URLRequestHeader = new URLRequestHeader("Content-type", "application/json");
var request:URLRequest = new URLRequest("http://localhost");
request.requestHeaders.push(hdr);

Hope this helped