2

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?

Andrei
  • 571
  • 6
  • 15

1 Answers1

8

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

Dave Mackintosh
  • 2,738
  • 2
  • 31
  • 40
  • Thank you for answer, but I already tried this way and got the same result. Content-Type is in request body but not in the headers. – Andrei Sep 11 '11 at 09:53
  • How is your JSON output? With PHP, ASP, ColdFusion, etc? If so; do you have control of that file? because I don't believe you'll find a solution to this and have to set the header within your JSON file before output. – Dave Mackintosh Sep 11 '11 at 10:37
  • Sorry for double commenting but it looks like you're trying to use Flash to alter headers that have already been sent? You can't do that, your post request is valid and the headers are correct? – Dave Mackintosh Sep 11 '11 at 10:38
  • After your answer I made some more tests and every thing is works fine. I made a mistake assuming that error was with headers, but actually it was in json string. I don't have control on server script, and I always got 400 code error. After some experiments with REST client: https://chrome.google.com/webstore/detail/hgmloofddffdnphfgcellkdfbfbjeloo I make it work right. So there is no problems with headers! – Andrei Sep 11 '11 at 11:40
  • Glad you got it solved, you could mark the answer as correct if you like :) – Dave Mackintosh Sep 11 '11 at 13:15