I'm trying to post JSON to server but code not working. I found some example to post JSON with Actionscripts 3 but i need to define content type in code. i post my test code here.
I tested url+header+body in firefox RESTClient and they working. [![enter image description here][1]][1] I'm sure URL is true and working in another places but here i got error "Error opening URL"
URL:"https://api.thinger.io/v2/users/***"
Content Header: "application/json"
Body: {"in":true}
import flash.display.Sprite;
import flash.events.*;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLRequestHeader;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;
var loader: URLLoader;
function ATN() {
loader = new URLLoader();
configureListeners(loader);
var header: URLRequestHeader = new URLRequestHeader("Accept", "application/json");
var request: URLRequest = new URLRequest("https://api.thinger.io/v2/users/***");
request.data = new URLVariables("in:true");
request.method = URLRequestMethod.POST;
request.requestHeaders.push(header);
try {
loader.load(request);
} catch (error: Error) {
trace("Unable to load requested document.");
}
}
function configureListeners(dispatcher: IEventDispatcher): void {
dispatcher.addEventListener(Event.COMPLETE, completeHandler);
dispatcher.addEventListener(Event.OPEN, openHandler);
dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
}
function completeHandler(event: Event): void {
var loader: URLLoader = URLLoader(event.target);
trace("completeHandler: " + loader.data);
}
function openHandler(event: Event): void {
trace("openHandler: " + event);
}
function progressHandler(event: ProgressEvent): void {
trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
}
function securityErrorHandler(event: SecurityErrorEvent): void {
trace("securityErrorHandler: " + event);
}
function httpStatusHandler(event: HTTPStatusEvent): void {
trace("httpStatusHandler: " + event);
}
function ioErrorHandler(event: IOErrorEvent): void {
trace("ioErrorHandler: " + event);
}
ATN();
ERROR:
Error: Error #2101: The String passed to URLVariables.decode() must be a URL-encoded query string containing name/value pairs.
at Error$/throwError()
at flash.net::URLVariables/decode()
at flash.net::URLVariables()
at ATN_fla::MainTimeline/ATN()
at ATN_fla::MainTimeline/frame1()
EDIT:
function ATN() {
loader = new URLLoader();
configureListeners(loader);
var header: URLRequestHeader = new URLRequestHeader("Accept", "application/json");
var request: URLRequest = new URLRequest("https://api.thinger.io/v2/users/***");
request.data = new URLVariables();
var postData: Object = {"in": true};
request.data = JSON.stringify(postData);
request.method = URLRequestMethod.POST;
request.requestHeaders.push(header);
try {
loader.load(request);
} catch (error: Error) {
trace("Unable to load requested document.");
}
}
OUTPUT:
openHandler: [Event type="open" bubbles=false cancelable=false eventPhase=2] progressHandler loaded:44 total: 44
EDIT 2:
function ATN() {
loader = new URLLoader();
configureListeners(loader);
var header: URLRequestHeader = new URLRequestHeader("Accept", "application/json");
var request: URLRequest = new URLRequest("https://api.thinger.io/v2/users/***");
var postData: Object = {"in": true};
request.data = JSON.stringify(postData);
request.method = URLRequestMethod.POST;
request.requestHeaders.push(header);
try {
loader.load(request);
} catch (error: Error) {
trace("Unable to load requested document.");
}
}
OUTPUT 2:
openHandler: [Event type="open" bubbles=false cancelable=false eventPhase=2] progressHandler loaded:44 total: 44
Error opening URL 'https://api.thinger.io/v2/users/***'
httpStatusHandler: [HTTPStatusEvent type="httpStatus" bubbles=false cancelable=false eventPhase=2 status=400 redirected=false responseURL=null]
ioErrorHandler: [IOErrorEvent type="ioError" bubbles=false cancelable=false eventPhase=2 text="Error #2032: Stream Error. URL: https://api.thinger.io/v2/users/***"]
The error is in parsing the string you are providing to the URLVariables constructor. URLVariables is only really for constructing name/value pairs. If you want to send JSON simply set the data property directly: