Flash / App Engine request data encoding problem (GET works, POST fails) - actionscript-3

I'm new to Flash and trying to communicate with an app engine server but I'm having some encoding problems.
var playerLoader:URLLoader = new URLLoader();
playerLoader.addEventListener( Event.COMPLETE, function(e:Event) { doStuff(); } );
var requestVars:URLVariables = new URLVariables();
requestVars.q = "åäö";
var urlRequest:URLRequest = new URLRequest( serverUrl );
urlRequest.method = URLRequestMethod.POST;
urlRequest.data = requestVars;
playerLoader.load( urlRequest );
This does not work, gives me some encoding errors on the server side. But if I switch POST to GET it automagically works.
Any clues what's going on and how I could use POST?
Thanks!

As far as I know, Actionscript's HTTP requests will natively be encoded in UTF-8.
I have it working this way for spanish chars:
var url:String = "historia.php";
var request:URLRequest = new URLRequest(url);
var requestVars:URLVariables = new URLVariables();
var now:Date = new Date();
requestVars.historia = "áóíúéñÁÓÍÚÉÑ";
request.data = requestVars;
request.method = URLRequestMethod.POST;
var urlLoader:URLLoader = new URLLoader();
urlLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
urlLoader.load(request);
And on the PHP side (resumed for clarity):
header ('Content-type: text/html; charset=utf-8');
if(isset($_POST['historia']) {
$historia = mysql_real_escape_string(utf8_decode($_POST['historia']));
$historia = filter_var($historia, FILTER_SANITIZE_STRING);
// insert into DB...
}
I use utf8_decode() because the database is not configured with encoding UTF-8.
How are you handling the request on the server side? Maybe the problem lies on the server side.

Related

Cancel a POST request

I have an app that makes a request to a server to generate an audio file and the problem that I have is that if I send two distinct and consecutive requests, I can not cancel the first. I tried with .unload or .unloadAndStop but always gives me error.
Any suggestions?
elfich="sonido" + elfichnum;
var request:URLRequest = new URLRequest();
var variables:URLVariables = new URLVariables();
var loader:URLLoader = new URLLoader();
variables.text=caja_texto;
variables.fileName=elfich;
request.url = "http://192.168.0.19:800/cgi-bin/tts.cgi";
request.method = URLRequestMethod.POST;
request.data = variables;
loader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, cargacompletanew);
loader.addEventListener(IOErrorEvent.IO_ERROR, onErrornew);
loader.load(request);
}
function cargacompletanew(event:Event):void
{
xlocalSound = new Sound();
var xreq:URLRequest = new URLRequest("http://192.168.0.19:800/output/" + elfich + ".mp3");
xlocalSound.load(xreq);
xlocalSound.addEventListener(Event.OPEN, iniciarSonido);
}
What about
loader.removeEventListener(Event.COMPLETE, cargacompletanew);
loader.removeEventListener(IOErrorEvent.IO_ERROR, onErrornew);
loader.close();
I think there was a problem with close() if you were trying to close it if nothing was loaded, so I'd try to put that inn try/catch block.
try
{
loader.close();
}
catch(e:Error)
{
loader("Oh noes!");
}

AS3 won't send POST data to browser - wrong URLRequestHeader content type?

I have the following code in my AS3 Flash code that takes a screenshot within the swf using JPGEncoder and sends it to the url where i write it to a file in PHP.
I did run into the Google Chrome Pepperflash issue recently where the function just stops and the page fails to redirect. Nothing gets sent to save.php. By changing
var header:URLRequestHeader = new URLRequestHeader ("Content-type", "application/octet-stream");
to
var header:URLRequestHeader = new URLRequestHeader ("Content-type", "text/plain");
That seemed to do the trick. As of today though this works in internet Explorer but no longer in Chrome, Safari, Firefox. I saw that Adobe put out an update/patch to flash and flash player yesterday - could that have anything to do with it?
If i remove the following:
var header:URLRequestHeader = new URLRequestHeader ("Content-type", "text/plain");
jpgURLRequest.requestHeaders.push(header);
Then the page successfully redirects but $GLOBALS['HTTP_RAW_POST_DATA'] is then empty so no image file can be created.
Is there an alternative header i can put that will solve this?
My code is:
AS3:
function createJPG(m:MovieClip, q:Number, fileName:String) {
var jpgSource:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight);
jpgSource.draw(stage);
var jpgScreenshot: BitmapData = new BitmapData(362, 310);
jpgScreenshot.copyPixels(jpgSource, new Rectangle(288, 89, 362, 310), new Point(0, 0));
var jpgEncoder:JPGEncoder = new JPGEncoder(q);
var jpgStream:ByteArray = jpgEncoder.encode(jpgScreenshot);
var header:URLRequestHeader = new URLRequestHeader ("Content-type", "text/plain");
var jpgURLRequest:URLRequest = new URLRequest ("http://www.url.com/save.php");
jpgURLRequest.requestHeaders.push(header);
jpgURLRequest.method = URLRequestMethod.POST;
jpgURLRequest.data = jpgStream;
var jpgURLLoader:URLLoader = new URLLoader();
navigateToURL(jpgURLRequest, "_self");
}
save.php
$imagefile=''.$imageURL.'';
$fp = fopen($imagefile, 'wb');
fwrite($fp, $GLOBALS['HTTP_RAW_POST_DATA']);
fclose($fp);
header('Location: https://www.url.com/your-image.php');
Managed to get this working now with the following code. Liam was correct on the Flash Player issue. Working now by separating saving the image and navigating to the url into 2 differents function:
function createJPG(m:MovieClip, q:Number, fileName:String) {
var jpgSource:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight);
jpgSource.draw(stage);
var jpgScreenshot: BitmapData = new BitmapData(362, 310);
jpgScreenshot.copyPixels(jpgSource, new Rectangle(288, 89, 362, 310), new Point(0, 0));
var jpgEncoder:JPGEncoder = new JPGEncoder(q);
var jpgStream:ByteArray = jpgEncoder.encode(jpgScreenshot);
var urlLoader:URLLoader = new URLLoader();
var header:URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream");
var saveJPG:URLRequest = new URLRequest('http://www.url.com/save_image.php?name=filename';
saveJPG.requestHeaders.push(header);
saveJPG.method = URLRequestMethod.POST;
saveJPG.data = jpgStream;
urlLoader.addEventListener(Event.COMPLETE, goToCheckout);
urlLoader.load(saveJPG);
}
function goToCheckout(e:Event):void{
var url:String = 'http://www.url.com/show_image.php';
var request:URLRequest = new URLRequest(url);
try {
navigateToURL(request, '_self');
} catch (e:Error) {
trace("Error occurred!:");
}
}
save_image.php:
if(isset($GLOBALS["HTTP_RAW_POST_DATA"])){
$jpg = $GLOBALS["HTTP_RAW_POST_DATA"];
$path = "";
$id = $_GET["name"];
$file = $id;
file_put_contents($path.$file, $jpg);
echo "complete";
} else{
// error
}
Flash Player version 13.0.0.214 introduces some key security fixes. Unfortunately it also breaks navigateToUrl() by forbidding the changing of any headers in the request given to navigateToUrl(). This breaks POST requests that need to pass in security/session token headers, or to even change the Content-Type, e.g. to text/xml, etc. as you have done in your example.
Right now, as much as it sucks, our best known workaround is to downgrade clients to 13.0.0.206
Adobe proposes using external interface call as described here: https://forums.adobe.com/message/6396080
it uses JavaScript to replace the POST method
The solution proposed by odd_duck seems simpler though - would be great to see the php code as well.

AS3 AIR URLLoader POST

I am developing my first AIR app (moving over from PHP/Javascript) and am at a stage where I want to send some data from the app back to a PHP script on my server. I have the following:
var url:String = "PHP URL HERE";
var request:URLRequest = new URLRequest(url);
var requestVars:URLVariables = new URLVariables();
requestVars.test = "1234";
request.data = requestVars;
request.method = URLRequestMethod.POST;
request.contentType = "application/xml;charset=utf-8";
var urlLoader:URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
urlLoader.addEventListener(Event.COMPLETE, processSERPS, false, 0, true);
urlLoader.load(request);
I did have something else in the server side PHP script originally but for debugging I just changed it to dump the REQUEST array.
With the code above it will simply return nothing:
Array
(
)
But if I change the request method to Get:
request.method = URLRequestMethod.GET;
I receive:
Array
(
[test] => 1234
)
Which tells me that the code is correct, it just isn't sending the post parameters for some reason.
I would just alter the code to use GET variables but unfortunately the data I need to send is too large hence the need for POST.
Any help appreciated!
For URLRequest.contentType:
If the value of the data property is a URLVariables object, the value
of contentType must be application/x-www-form-urlencoded.
This is the default value, so just removing your assignment should do it.

Sending data from flash to php using post method as2

I am using following code in as3:
var Request:URLRequest = new URLRequest();
var xmlMain:XML=new XML();
var urlXmlLoader:Loader = new Loader();
var myVars:URLVariables = new URLVariables();
myVars.RequestXml = xmlMain;
Request.url = "http://www.xyz.php"; //some php url
Request.method = URLRequestMethod.POST;
Request.data = myVars;
urlXmlLoader.dataFormat = URLLoaderDataFormat.TEXT;
urlXmlLoader.addEventListener(Event.COMPLETE, getData);
urlXmlLoader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
urlXmlLoader.load(Request);
Now I have an as2 project where I need to do the same. How can it be done in as2??
This is probably the best tutorial on HTTP requests in AS2: http://www.sephiroth.it/tutorials/flashPHP/loadVars/

LocalConnection var into URLRequest

i use LocalConnection between two swf in the same page.
What i'm trying to do is to use a String sent from one swf to the other, as variable into my URLRequest...
So i can trace "myVar" into the function chemin, but i didn't find how to use it into URLRequest
thank's for your help
swf that receive the var :
var lc:LocalConnection=new LocalConnection();
lc.client=this;
lc.connect("callBig");
function chemin(myVar:String){
trace(myVar)
}
var chargementXML:URLLoader = new URLLoader();
var fichier:URLRequest = new URLRequest(myVar);
Some references to help you out.
URLRequest.data: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/URLRequest.html?filter_flex=4.1&filter_flashplayer=10.2&filter_air=2.6#data
URLVariables: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/URLVariables.html
So, if you want to pass some variables in your request, you do the following:
Create new URLVariables() object
Assign it's reference to URLRequest.data field.
var fichier:URLRequest = new URLRequest();
var urlVars:URLVariables = new URLVariables();
urlVars["myVarName"] = myVar;
fichier.data = urlVars;
This way your server side app will recieve a variable myVarName with value of myVar.
var lc:LocalConnection=new LocalConnection();
lc.client=this;
lc.connect("callBig");
var receivedVar:String = "";
// somewhere else
function chemin(myVar:String){
receivedVar = myVar;
}
// later
var chargementXML:URLLoader = new URLLoader();
var vars:URLVariables = new URLVariables();
vars.myVar = receivedVar;
chargementXML.data = vars;
var fichier:URLRequest = new URLRequest(myVar);