Cannot use URLRequestMethod different from POST GET in ActionScript - actionscript-3

I'm trying to get the head of a page without downloading the whole thing, because it's too heavy and I just need the header information.
The code is this:
var resource:String = "<myResource>";
var urlRequest : URLRequest = new URLRequest(resource);
urlRequest.method = URLRequestMethod.HEAD;
var requestLoader:URLLoader = new URLLoader();
requestLoader.load(urlRequest);
It works if I use either POST or GET, but not HEAD, PUT or DELETE.
I don't know how to solve it or if it has been deprecated.
According to the docs, the constant is still valid but in the first line it says that it is only to distinguish between GET and POST.
Edit:
Finally I ended up doing the request in Javascript and calling it from actionScript. Maybe it is not a great solution, but it works fine.

Related

Get mime/type from MediaPromise

I am trying to make my scrips as cross-platform as possible. I am using CameraUI to fetch files and upload them to Firebase. The problem is I can only get the bytearray through the filepromise and not the file extension..or anything. According to numerous guides iOS wont let you get mediaPromise.file to get it's type. So I'm left with the question of how to get a mime/type from this bytearray I have, that I know is either an image or a video from MediaPromise.type.
MetaData or anything would help.
Following this guide gets me to a security error. Something with domains. I'm in Android and iOS so I can't do Security.allowDomain("*"); I made my own version by following this guide.
I searched and found this guide, and this works. But it only fetches the bytearray..
He wrote another article that made something that extracted data from the first 64K of the bytearray or something and displayed it in his app. He used a lib that's no longer up so I can't really go with his guide.
The code in there is the code I have in my script aside from a few UI management additions. How would I go about solving this? Is there some meta-data that always lies in the first set of bytes?
I even tried using this nice ANE, but I get an error..
You can get the mime type once the mediapromise was loaded:
cameraUI = new CameraUI();
if (CameraUI.isSupported)
{
cameraUI.addEventListener(MediaEvent.COMPLETE, onCameraUIComplete);
cameraUI.addEventListener(Event.CANCEL, onCameraUICanceled);
cameraUI.addEventListener(ErrorEvent.ERROR, onCameraError);
cameraUI.launch(MediaType.IMAGE);
}
private function onCameraUIComplete(e:MediaEvent):void
{
var mediaPromise:MediaPromise = e.data;
if (mediaPromise)
{
myLoader = new Loader();
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onMediaPromiseLoaded);
myLoader.addEventListener(IOErrorEvent.IO_ERROR, onMediaPromiseLoadError);
myLoader.loadFilePromise(mediaPromise);
}
}
private function onMediaPromiseLoaded(e:Event):void
{
var myLoaderInfo:LoaderInfo = e.target as LoaderInfo;
var myByteArray:ByteArray = myLoaderInfo.bytes;
var mimeType:String = myLoaderInfo.contentType;
}
Another way is to determine the mimetype "manually" based on the file extension in mediaPromise.file.extension

AS3 URLRequest POST Only Works When Compiled in HTML Wrapper

I'm attempting to send a POST request with data to another domain with this code:
_snapshot_id = 1369022400;
var urlRequest:URLRequest = new URLRequest("https://fuzzykittens/radar");
urlRequest.method = URLRequestMethod.POST;
urlRequest.contentType = "application/x-www-form-urlencoded";
//set variables for post
var postVars:URLVariables = new URLVariables();
postVars.snapshot = String(_snapshot_id);
urlRequest.data = postVars;
//initialize weather proccess request
weatherProcRequest = new URLLoader(urlRequest);
weatherProcRequest.addEventListener(Event.COMPLETE,
weatherProcRequest_CompleteHandler);
weatherProcRequest.addEventListener(IOErrorEvent.IO_ERROR,
weatherProcRequest_ErrorHandler);
weatherProcRequest.addEventListener(SecurityErrorEvent.SECURITY_ERROR,
weatherProcRequest_ErrorHandler);
weatherProcRequest.load(urlRequest);
When I set the flex compiler to use an HTML wrapper, the request works. When I don't use a wrapper, the request throws an io error #2032. I think it's not sending the snapshot id, but I don't know why.
Is there any obvious reason why a request would send data when debugged in an html wrapper and fail to do so when debugged outside an html wrapper?
fuzzykittens has a crossdomain.xml with
<allow-access-from domain="*" secure="false"/>
I found the issue. Adobe very briefly mentions this caveat in URLRequest's livedoc:
Note: If running in Flash Player and the referenced form has no body, Flash Player automatically uses a GET operation, even if the method is set to URLRequestMethod.POST. For this reason, it is recommended to always include a "dummy" body to ensure that the correct method is used.
When I just ran the SWF without a body, the snapshot id was not sent as a post var so the server responded differently.
It's definitely not the answer I wanted, but it feels good to know what was going on. Thank you for your comments.

Need help parsing through a page & getting all comments found into an Array using a RegEx pattern in Google Apps Script?

My problem lies in 2 parts however I'm hoping solving 1 will fix the other. I've been trying to parse through a page and get all the comments found within a forum thread.
The comments are found using a RegEx pattern and the idea is that whatever lies in the comment will be read into an array until there aren't any more comments left. Each comment div follows this format
<div id="post_message_480683" style="margin-right:2px;"> something </div>
I'm trying to locate up to "post_message_[some number]" since each number seems to be generated randomly and then get whatever is between that particular div. My 1st problem is my RegEx just doesn't seem to be working I've tried a few but none yielded any results (except for when I insert the post message no. in manually), Here's the code so far:
function GetPosts() {
var posts = new Array(60);
var url = "http://forums.blackmesasource.com/showthread.php?p=480683";
var geturl = UrlFetchApp.fetch(url).getContentText().toString();
var post_match = geturl.match(/<div id="post_message_(.+)" style="margin-right:2px;">(\w.+)<\/div>/m);
Logger.log(post_match);
}
Edit: I initially tried getting this info via GAS's Xml.Parse() class but after grabbing the URL I just didn't know what to do since suffixing
.getElement().getElement('div') (I also tried .getElements('div') and other variations with 'body' & 'html')
would cause an error. Here is the last code attempt I tried before trying the RegEx route:
function TestArea() {
var url = "http://forums.blackmesasource.com/showthread.php?p=480683";
var geturl = UrlFetchApp.fetch(url).getContentText().toString();
//after this point things stop making sense
var parseurl = Xml.parse(geturl, true);
Logger.log(geturl);
//None of this makes sense because I don't know HOW!
//The idea: Store each cleaned up Message Div in an Array called posts
//(usually it's no more than 50 per page)
//use a for loop to write each message into a row in GoogleSpreasheet
for (var i = 0; i <= parseurl - 1; i++) {
var display = parseurl[i];
Logger.log(parseurl); }
}
Thanks for reading!
In general like the comment points out - be aware of parsing HTML with RegEx.
In my past personal experience, I've used Yahoo's YQL platform to run the HTML through and using XPath on their service. Seems to work decently well for simple reliable markup. You can then turn that into a JSON or XML REST service that you can grab via UrlFetch and work on that simplified response. No endorsement here, but this might be easier than to bring down the full raw HTML into Google Apps Script. See below for the YQL Console. I also don't know what their quotas are - you should review that.
Of course, the best course is to convince the site owner to provide an RSS feed or an API.

POST file upload using URLRequest

I have a quick question regarding POST file uploads in ActionScript 3. I am trying to upload a ByteArray from memory via POST to a server. I'm using the URLRequest class to send the data, and URLLoader because I want to monitor the progress. The relevant sections of code follows:
var uploadRequest:URLRequest = new URLRequest("http://127.0.0.1/upload.php");
uploadRequest.method = URLRequestMethod.POST;
uploadRequest.contentType = "multipart/form-data";
uploadRequest.data = myByteArray;
var uploader:URLLoader = new URLLoader;
uploader.addEventListener(ProgressEvent.PROGRESS, onUploadProgress);
uploader.addEventListener(Event.COMPLETE, onUploadComplete);
uploader.dataFormat = URLLoaderDataFormat.BINARY;
uploader.load(uploadRequest);
The problem is that I've set my callbacks to trace the upload progress, and the bytesTotal property of the ProgressEvent is always 1960 (the size of the request minus data?), even though the actual data is around 20MB and no file is uploaded even after the Complete event fires.
I've verified that upload.php functions correctly with a simple html form, and I can verify that myByteArray contains all of the data in question. Can anyone tell me what I'm doing wrong?
Edit:
I've attempted a couple of new things that I thought I should mention. The first is setting the content type to application/octet-stream instead of multipart/form-data, which had no effect other than increasing the number of bytes to 1964. I also checked the Apache error log and found the following text repeated a lot:
PHP Warning: Missing boundary in multipart/form-data POST data in Unknown on line 0
I'm guessing that Flash isn't formatting the HTTP request properly for whatever reason. Given that I created a FileReference that makes use of the same methods I set for the URLLoader to upload a file from disk, and got the expected result: the bytesTotal property matched the file size and the file was uploaded correctly.
Working from that I found a page in the Adobe developer docs that mentions uploading data to a server using FileReference.upload() by setting the data parameter of the URLRequest, so I tried the following code:
var uploadRequest:URLRequest = new URLRequest("http://127.0.0.1/upload.php");
uploadRequest.method = URLRequestMethod.POST;
uploadRequest.data = myByteArray;
fileRef = new FileReference;
fileRef.addEventListener(ProgressEvent.PROGRESS, onUploadProgress);
fileRef.addEventListener(Event.COMPLETE, onUploadComplete);
fileRef.upload(uploadRequest);
Which resulted in the following output:
ArgumentError: Error #2127: FileReference POST data cannot be type ByteArray.
I'm really stuck here. Any suggestions would be appreciated!
You should add more info to the "Content-Type" header:
uploadRequest.contentType = "multipart/form-data; boundary=<<boundary here>>";

What would cause Flash (AS3) to substitute in & for & in GET query params?

I've spent a day on this so far and just can't figure it out. The problem is that Flash is apparently substituting & for & in my GET query params.
So for example, I'm sending into a URLLoader a URLRequest with a url similar to this:
http://foo.blah.com/app/index.php?abc=def&ghi=jkl&mno=pqr
I verified this by looking at the var in the debugger. Yet it's failing on our server. Ethereal reports that what is actually going over the wire is this:
GET /app/index.php?abc=def&ghi=jkl&mno=pqr
Not the first, but the second & is being replaced with &.
I can't figure out how to make it stop doing this. What could cause this? Or does anyone have any ideas of things I can investigate?
Use the data property to construct the passed GET variables (you will have to set the request method to GET as well, check out the AS3 documentation for more granular information).
example :
var getVars:URLVariables = new URLVariables();
getVars.abc = "def";
getVars.ghi = "jkl";
var request:URLRequest = new URLRequest();
request.data = getVars;
urlLoader.load(request);
Ugh. Ok, thanks to Theo (who I'll give credit for the answer because I didn't know about URLVariables) I went and checked every possible thing that could be wrong.
The real problem was that I do indeed have & in the original URL request. I was doing a string.replace() that of course only hits the first instance.
The problem was that in the debugger, Flash apparently helpfully converts & back to &! So I didn't see the bug. Thanks Flash!
If I had done a trace() on the output just to be sure (which I thought I had but I clearly hadn't) then I would have noticed this.