2

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/***"]

vimuth
  • 5,064
  • 33
  • 79
  • 116
A.M
  • 345
  • 4
  • 13
  • That `Error #2032: Stream Error. URL: https://api.thinger.io/v2/users/***`tells you that the URL was not found (URL does not exist). (2) Is **https://api.thinger.io/** supposed to be valid URL? I get shown a _"404 Not Found"_ so how could folders like `v2` and `users` exist there? – VC.One Sep 15 '17 at 14:49
  • URL work fine. I tested it in firefox RESTClient. here i removed some part of URL. – A.M Sep 15 '17 at 18:59
  • I changed JASON data and previous error disappeared. now new error appears. – A.M Sep 15 '17 at 20:18

1 Answers1

0

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:

var postData:Object = { "in": true };
request.data = JSON.stringify( postData );
Michael
  • 3,776
  • 1
  • 16
  • 27
  • Thanks for answer, I changed code similar to EDIT section but error occurred. Is my code change true? – A.M Sep 16 '17 at 04:50
  • What error occurred? You didn't provide any information about your issue – Michael Sep 16 '17 at 07:29
  • Is my new code true? You can see new error below EDIT section. – A.M Sep 16 '17 at 09:56
  • We'll you're setting the request data twice now, you should remove the first reference. That doesnt look like an error just the trace output from your code? – Michael Sep 16 '17 at 09:58
  • I updated this post. please check it. It's not really error. it appears in output window. – A.M Sep 16 '17 at 10:04
  • It seems server or URL have problem but I'm sure URL and SERVER works fine. – A.M Sep 16 '17 at 10:10