Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set JSON content-type on s:HttpService in flex

I am trying to set the json content type on httpservice to make REST service return the json data. When I add the content type in fiddler all works fine so the problem lays in flex application, not in the web-service. But the code below does not work and I get the xml data instead of json.

Could anyone provide me the workaround/solution?

mxml:

<s:HTTPService id="service" method="POST" url="server.com" 
               result="loaded(event)" fault="fault(event)" 
               useProxy="false" resultFormat="text">

actionscript:

public function loadAllSamples():void {
    service.contentType = "application/json";
    service.send('something');
}
like image 204
igu Avatar asked Nov 16 '10 16:11

igu


2 Answers

Looks like I have sorted it out. The trick is that Accept header should be added on service:

       var header:Object=new Object();

        **header["Accept"] = "application/json";**

        service.contentType = "application/json";
        service.headers = header;
        service.send('{}');

I wish it could be helpful for somebody. Good luck.

like image 134
igu Avatar answered Sep 21 '22 16:09

igu


Thanks, this was very helpful to me. I simplified the header assignment to:

httpService.headers = { Accept:"application/json" };

like image 34
Cookie Avatar answered Sep 21 '22 16:09

Cookie