4
votes

I am calling a server method through HTTPService from client side. The server is a RestFul web service and it might respond with one of many HTTP error codes (say, 400 for one error, 404 for another and 409 for yet another). I have been trying to find out the way to determine what was the exact error code sent by the server. I have walked teh entire object tree for the FaultEvent populated in my fault handler, but no where does it tell me the error code. Is this missing functionality in Flex?

My code looks like this: The HTTP Service declaration:

    <mx:HTTPService id="myServerCall" url="myService" method="GET" 
resultFormat="e4x" result="myServerCallCallBack(event)" fault="faultHandler(event)">
            <mx:request>
                <action>myServerCall</action>
                <docId>{m_sDocId}</docId>
            </mx:request>
        </mx:HTTPService>

My fault handler code is like so:

private function faultHandler(event : FaultEvent):void
{
 Alert.show(event.statusCode.toString() + " / " + event.fault.message.toString()); 
}
5

5 Answers

4
votes

I might be missing something here, but:

event.statusCode

gives me the status code of the HTTP response.

So I can successfully do something like this in my fault handler function:

public function handleFault(faultEvent:FaultEvent):void
{
    if (faultEvent.statusCode == 401)
    {
        Alert.show("Your session is no longer valid.", "", Alert.OK, this, loginFunc);
    }
    else
    {
        Alert.show("Failed with error code: " + faultEvent.statusCode as String);
    }
}
3
votes

Looks like you are out of luck: http://fantastic.wordpress.com/2007/12/26/flex-is-not-friendly-to-rest/

You may have to use ExternalInterface to get this handled in JS and then communicated to Flex.

1
votes

The Flash Player needs help from the browser to be able to access the HTTP status code; therefore, this is not available on all platforms. For me, it failed with Flash Player 10.3.183.11 and Firefox 3.6.26, but worked with IE 8 on Windows 7.

The Adobe help for the FaultEvent.statusCode property hints at this, but unfortunately doesn't go into details:

this property provides access to the HTTP response status code (if available), otherwise the value is 0

So, if you absolutely need the status code, bad luck; if it's just to generate a better or friendlier error message for some frequent error conditions, it may be sufficient.

1
votes

as3httpclient as posted by Ross is friendly to Rest, and provides you with the HTTP status code, as long as you're developing for AIR and not a browser-based app.

I could not get as3httpclient to work from the browser, even when making requests to the same origin. There's documentation stating you need to set up a socket policy file server to get this to work. Not scalable for our uses so I setup a Proxy web service on the same host running the flex app.

I use HTTPService to make the call to the proxy web service, which forwards the request to the destination, and the proxy web service returns the http status code and message body back to the HTTPService in xml.

0
votes