send post with AS 3.0 - actionscript-3

I want to submit a form in flash and send the user to an other page in html/php and get the post information on that page. How can i do this?

// create a URLRequest object with the target URL:
var url : String = 'newpage.html';
var urlRequest : URLRequest = new URLRequest(url);
// create a URLVariables object and add all the values you want to send with their identifiers:
var urlVariables : URLVariables = new URLVariables();
urlVariables['formfieldId'] = 'formfieldValue';
// add the urlVariables to the urlRequest
urlRequest.data = urlVariables;
// set the method to post (default is GET)
urlRequest.method = URLRequestMethod.POST;
// use navigateToURL to send the urlRequest, use '_self' to open in the same window
navigateToURL(urlRequest, '_self');

Use the URLRequest class

Related

Sending data to a JSON with AS3

I've asked my client to share his database login and password but he can't give me full access to his database (security reason I suppose).
He told me to use a REST/JSON service that allows to post the data via this url with a specific key that allows him to identify all the datas coming from my app.
Here's what I did :
var urlRequest:URLRequest = new URLRequest("the_url_using JSON service");
urlRequest.method = URLRequestMethod.POST;
var urlvars: URLVariables = new URLVariables;
urlvars.observer_name = "Test Coco";
urlvars.observation_number = "5433";
trace("urlvars = "+urlvars);
urlRequest.data = urlvars;
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, onComplete);
urlLoader.load(urlRequest);
It's working, as it's sending the data, but the data format seems to be incorrect..
the url returns this error : "Observer name is Missing"
And the "trace (urlvars)" output :
urlvars = observer%5Fname=Test%20Coco&observation%5Fnumber=5433
So I think the problem come from the special character or something like that (as you can "observer_name" results by "observer%5Fname" and we can see a lot of %5")
Any idea how can I solve this ?
JSON string is a string representation of a generic object. Basically you go:
var anObject:Object =
{
"observer_name": "Test Coco",
"observation_number": 5433
};
or you can construct it
var anObject:Object = new Object;
anObject['observer_name'] = "Test Coco";
anObject['observation_number'] = 5433;
and then you convert it to String and attach to request
var jsonString:String = JSON.stringify(anObject);
urlRequest.method = URLRequestMethod.POST;
urlRequest.data = jsonString;
Read more about it: https://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/JSON.html
Keep in mind that I don't know the specifics of that REST server of yours and the code above just might not work as it is. I only explain how to send a JSON string as a POST request.

Trying to get user timeline from twitter using application-only authentication

I'm trying to get a simple app working from within Flash to see a users tweets.
I 'believe' I have correctly gotten the access token.
var bearerToken:String;
var consumerKey:String = <CONSUMER_KEY>;
var consumerSecret:String = <CONSUMER_SECRET>;
//encode key and secret according to twitter's website
var bearerTokenCredentials = Base64.encode(consumerKey + ':' + consumerSecret);
//setup my request variables, according to twitters dev info, use client_credentials for grant_type
var requestVars:URLVariables = new URLVariables();
requestVars.grant_type = 'client_credentials';
//according to twitter, the url to use for application-only authenticaton
var url:String = 'https://api.twitter.com/oauth2/token';
//setup a URLRequest object with the proper headers, according to twitter, need the Authorization header to be 'Basic <encoded key/secret>' and the proper Content-Type header
var request:URLRequest = new URLRequest(url);
request.requestHeaders = [new URLRequestHeader('Authorization', 'Basic ' + bearerTokenCredentials),
new URLRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8')];
request.method = URLRequestMethod.POST;
request.data = requestVars;
//send the request to authenticate
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, onBearerTokenRequest);
urlLoader.load(request);
function onBearerTokenRequest(e:Event):void
{
trace('onRequestComplete');
//this works and i get a token
bearerToken = JSON.decode(e.currentTarget.data).access_token;
//HERE IS WHERE I CAN'T FIGURE OUT WHAT'S WRONG
var url:String = 'https://api.twitter.com/1.1/statuses/user_timeline.json'
var urlVariables = new URLVariables();
urlVariables.screen_name = 'rivercitygraphx';
urlVariables.count = 2;
var request:URLRequest = new URLRequest(url);
request.method = "GET";
request.requestHeaders = [new URLRequestHeader('Authorization', 'Bearer ' + bearerToken)];
request.data = urlVariables;
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, onTimelineRequestComplete);
urlLoader.load(request);
}
function onTimelineRequestComplete(e:Event):void
{
trace('onTimelineRequestComplete');
}
I get the token back properly (well I assume so since I get a long string).
But when I try to make the request for the user timeline I get an ioError Error opening URL 'https://api.twitter.com/1.1/statuses/user_timeline.json?screen%5Fname=kamcknig&count=2' which makes me think the URL is malformed, but according to everytihng I see, it seems correct.
Also, right now I'm just testing this within the Flash IDE.
Can anyone help??
I have since used the POSTMAN extension in Chrome and entered the url, and the Authorization header with the bearer token that I received and the request worked within POSTMAN!
So the only thing that i can think is it's the Flash IDE. Anyone have any ideas why that might be or if that might actually even be the problem?
OK after all my research it looks like this is impossible.
I have found a library that uses a socket connection that can do what I want, but the filesize is far too large. The Socket class in AS3 is not a secure socket so I can't use it. And the SecureSocket class can't be used either since I am limited to Flash Player 10 and earlier.
So as far as I'm concerned, this is impossible.

specifying the "request method" in externalinterface.call method?

When the user try to download the file ,i need to open a new html window and download the file , currently iam using "ExternalInterface.call" but i need to set the "requestmethod" otherwise server is throwing the error ,how to specify "requestmethod"?
Why not use the global navigateToURL method? You can pass in a URLRequest object which has a property called method and will solve your problem.
So you need something like this:
var request:URLRequest = new URLRequest("http://example.com");
request.method = URLRequestMethod.POST; // or anything you want
// parameters can be added to a URLVariables object
// which then can be set as requests data property
// example:
var params:URLVariables = new URLVariables();
params.username = "test";
params.password = "123";
request.data = params;
// load the page
// specify '_blank' parameter to open it in a new browser window
navigateToURL(request, "_blank");

URLRequest/URLLoader auto-converting POST request to GET

When I execute the following code:
var urlRequest:URLRequest = new URLRequest("http://somehost/with/some/path?andsomequerystring=true");
urlRequest.method = 'POST';
var urlLoader:URLLoader = new URLLoader(urlRequest);
urlLoader.addEventListener(Event.COMPLETE, function(event:Event):void{
trace('sweet');
});
It turns my explicit POST request to GET due to the presence of the querystring. If I remove the querystring (and serialize as part of the POST body), it successfully makes the corresponding POST request. Is there any way to prevent it from doing that? My server requires that a POST request be made with a querystring.
Ah think I found the answer, seems you have to specify a body as well or else it will still send as a GET request from their docs:
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.
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/URLRequest.html#url
It is because the way you are having your url. If you want to have your variables for POST method you need to use URLVariables.
var urlRequest:URLRequest = new URLRequest(YOUR_REQUEST_URL_HERE);
var variables:URLVariables = new URLVariables();
variables.andsomequerystring = true;
urlRequest.data = variables;
urlRequest.method = 'POST';
var urlLoader:URLLoader = new URLLoader(urlRequest);
urlLoader.addEventListener(Event.COMPLETE, function(event:Event):void{
trace('sweet');
});

upload a zip file using HTTP POST via actionscript 3.0

I have a zip file that is created using drag and drop on a view in my desktop Flex 4.6 app.
This triggers a service that will automatically upload the zip file.
I am able to use the following code to send metadata about the zip file to the server.
var urlRequest:URLRequest = new URLRequest(PUBLISH_ZIP_FILE_URL);
// set to method=POST
urlRequest.method = URLRequestMethod.POST;
var params:URLVariables = new URLVariables();
params['data[File][title]'] = 'Title1';
params['data[File][description]'] = 'desc';
// params['data[File][filename]'] = I am not sure exactly what to use here
// If this is a webpage, I expect to use input type="file" with the name as data[File][filename]
urlRequest.data = params;
addLoaderListeners();
// set it such that data format is in variables
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.load(urlRequest);
I have read https://stackoverflow.com/questions/8837619/using-http-post-to-upload-a-file-to-a-website
However, immediately they start off with ByteArray, which I am not sure how to convert my zip file at all.
Please advise.
Embarrassing but I found my answer 42 mins after I posted the question.
A little bit of a rubber duck problem solving going on here.
http://www.codinghorror.com/blog/2012/03/rubber-duck-problem-solving.html
Short answer: Use File class and specifically the method upload which is extended from the FileReference class.
Long answer:
var urlRequest:URLRequest = new URLRequest(PUBLISH_ZIP_FILE_URL);
// set to method=POST
urlRequest.method = URLRequestMethod.POST;
var params:URLVariables = new URLVariables();
params['data[File][title]'] = 'Title1';
params['data[File][description]'] = 'desc';
// this is where we include those non file params and data
urlRequest.data = params;
// now we upload the file
// this is how we set the form field expected for the file upload
file.upload(urlRequest, "data[File][filename]");