Huuuze
Huuuze

Reputation: 16367

How do I read a Django HTTPResponse in Flex?

I'm a complete Flex noob, so I apologize in advance if I'm missing something obvious.

I wrote a fairly simple file uploader in Flex, which calls my Django back-end via URLRequest (the FileReference object handles the upload). My upload works as intended and I have Django return a HTTPResponse object. As such, I'd like to read the contents of the HTTPResponse object.

Any thoughts?

Upvotes: 2

Views: 1370

Answers (4)

PD.
PD.

Reputation: 51

I am also new to flex and I ran in the same problem when doing an upload to a Java Rest backend, I solved it using the DateEvent on the FileReference. To get the response data use something like this.:

var fileRef:FileReference = new FileReference();
fileRef.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, responseHandler);
var request:URLRequest = new URLRequest("yourUrl");
fileRef.upload(request, "fileData"); 

private function responseHandler(event:DataEvent):void {
    var response:XML = new XML(event.data); 
//Note the DataEvent: this is the event that holds the response. 
//I sent back data as xml
}

Your response should always be a successful HTTP status code (200), if your backend sends status 500 codes it will not trigger the DateEvent. Server errors can still be caught with a HTTPStatusEvent, but then you don't have access to the response.

Upvotes: 1

Marty Henderson
Marty Henderson

Reputation: 95

you can access the response like so in your onComplete event handler:

private function saveCompleteHandler(event:Event):void {
    var loader:URLLoader = event.currentTarget as URLLoader;
    trace("saveCompleteHandler - event returned:" + loader.data as String);
}

we do this this to get json fron a java web service.

you just need to use a URLLoader to load the URLRequest in the first place:

var loader:URLLoader = new URLLoader();
loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, statusHandler, 10000);
loader.addEventListener(IOErrorEvent.IO_ERROR, saveErrorHandler, 10000);
loader.addEventListener(Event.COMPLETE, saveCompleteHandler, 10000);

var request:URLRequest = new URLRequest("http:/whereverer");
request.method = URLRequestMethod.GET;
loader.load(request);

Upvotes: 0

kenneth
kenneth

Reputation: 1065

something along the lines of

<mx:HTTPService id="myHTTPRequest" 
    url="{whatever your url request is}"
    result="resultHandler(event)" 
    fault="faultHandler(event)"
    showBusyCursor="true" 
    resultFormat="object"> 

then inside the resultHandler something like this

private function resultHandler (event : ResultEvent) : void {
    var obj : Object = event.result;
    //do something with returned object

}

Debug at the point of the resultHandler to see exaclty whats being returned, make sure its what you think should be getting returned.

Upvotes: 2

Andy Baker
Andy Baker

Reputation: 21577

By the time it gets to the client it's just a normal HTTP response so treat it like any other response

Upvotes: 2

Related Questions