Search code examples
actionscript-3jsonactionscriptcontent-typeurlrequest

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


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?


Solution

  • 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