How to get HTTP status code in HTTPService error handler

I am calling the server method via HTTPService from the client side. The server is a RestFul web service and can respond with one of many HTTP error codes (say 400 for one error, 404 for another, and 409 for another). I tried to figure out a way to determine the exact error code sent by the server. I went through the entire object tree for the FaultEvent populated in my error handler, but no where it will tell me the error code. Is this missing functionality in Flex?

My code looks like this: 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 error handler code looks like this:

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

Something may be missing for me, but:

event.statusCode

gives me the HTTP response status code.

Therefore, I can successfully do something like this in my error 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); } } 
+4
source

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

You may need to use ExternalInterface to process it in JS and then pass it to Flex.

+3
source

Flash Player needs browser help to access the HTTP status code; therefore it is not available on all platforms. This failed for me with Flash Player 10.3.183.11 and Firefox 3.6.26, but it worked with IE 8 in Windows 7.

Adobe help for the attributes of the FaultEvent.statusCode property, but unfortunately does not 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 a status code, failure; if it's just to create a better or more convenient error message for some common errors, that might be enough.

+1
source

as3httpclient , as published by Ross, is Rest-friendly and provides you with an HTTP status code if you are developing for AIR, not a browser.

I could not get as3httpclient to work from the browser, even when requests for the same thing happened. There's documentation in which you need to configure a socket policy file server to get this to work. It is not scalable for our purposes, so I configure the Proxy web service on the same host as the flex application.

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

+1
source

Try using this instead of HTTPService: http://code.google.com/p/as3httpclient/

0
source

Source: https://habr.com/ru/post/1306087/


All Articles