I would like to load an api in ActionScript. It is at this link https://market.mashape.com/vivekn/sentiment-3 The curl request is this:
curl -X POST --include 'https://community-sentiment.p.mashape.com/text/' \
-H 'X-Mashape-Key: YourPrivateApiKeyHere' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H 'Accept: application/json' \
-d 'txt=Today is a good day'
How would I use this in AS3. Thanks in Advance.
You can use the URLLoader class to make REST calls.
Map the api's custom headers to URLRequestHeader classes and the custom http options to a URLRequest class.
Example trace output:
[trace] {
[trace] "result": {
[trace] "confidence": "72.7805",
[trace] "sentiment": "Negative"
[trace] }
[trace] }
[trace] 72.7805
[trace] Negative
ActionScript cut/paste example (just add your private mishap api key):
package {
import flash.display.Sprite;
import flash.text.TextField;
import flash.events.Event;
import flash.events.HTTPStatusEvent;
import flash.events.SecurityErrorEvent;
import flash.events.IOErrorEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLRequestHeader;
import flash.net.URLRequestMethod;
import flash.net.URLLoaderDataFormat;
public class Main extends Sprite {
var textField1:TextField = new TextField();
var textField2:TextField = new TextField();
var textField3:TextField = new TextField();
public function Main() {
textField1.text = "Today is a bad day";
addChild(textField1);
textField2.x = 150;
addChild(textField2);
textField3.x = 200;
addChild(textField3);
var apiKey:String = "your api key here";
var url:String = "https://community-sentiment.p.mashape.com/text/";
var headers:Array = [
new URLRequestHeader("X-Mashape-Key", apiKey),
new URLRequestHeader("Accept", "application/json"),
new URLRequestHeader("Content-Type","application/x-www-form-urlencoded")
];
var request:URLRequest = new URLRequest(url);
request.method = URLRequestMethod.POST;
request.requestHeaders = headers;
request.data = "txt=" + textField1.text;
var urlLoader:URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
urlLoader.addEventListener(Event.COMPLETE, httpRequestComplete, false, 0, true);
urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, httpRequestError, false, 0, true);
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, httpRequestError, false, 0, true);
urlLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler, false, 0, true);
urlLoader.load(request);
}
private function httpRequestComplete(event:Event):void {
trace(event.target.data);
var response:Object = JSON.parse(event.target.data);
trace(response.result.confidence);
trace(response.result.sentiment);
textField2.text = response.result.confidence;
textField3.text = response.result.sentiment;
}
private function httpRequestError(event:IOErrorEvent):void {
trace("Error: " + event.errorID + " " + event.text);
}
function httpStatusHandler(event:HTTPStatusEvent):void {
if (event.status != 200) {
trace("httpStatusHandler: " + event.status);
}
}
}
}