Hi,
I’m playing about with the habitRPG api, and trying to make an app in flash which uses it.
The following:
curl -X POST --compressed -H “Content-Type:application/json”
-H ‘x-api-user: xxxxxxxxxxxxxxxxxx’
-H ‘x-api-key: xxxxxxxxxxxxxxxxxxx’
-d ‘{“text”:“from the api!”,“type”:“todo”}’
https://habitrpg.com/api/v1/user/task
adds a task to my todo list, so far I’ve got this:
public function newTask():void
{
var todo:Array = new Array ();
todo.push ( { "text":"from the as3 api!", "type":"todo" } );
var JsonObj:String = JSON.stringify(todo);
trace(JsonObj);
//From here
var RequestURL = "https://habitrpg.com/api/v1/user/task";
var JSONLoader:URLLoader = new URLLoader();
JSONLoader.dataFormat=URLLoaderDataFormat.TEXT;
JSONLoader.addEventListener(IOErrorEvent.IO_ERROR, GetBookmarkURLError, false, 0, true);
JSONLoader.addEventListener(Event.COMPLETE, postSuccess, false, 0, true);
var hdr:URLRequestHeader = new URLRequestHeader("Content-type", "application/json");
var hdr2:URLRequestHeader = new URLRequestHeader("x-api-user", "xxxxxxxxxxxxxxxx");
var hdr3:URLRequestHeader = new URLRequestHeader("x-api-key", "xxxxxxxxxxxxxxxxx")
var request:URLRequest = new URLRequest(RequestURL);
request.requestHeaders = [new URLRequestHeader("Content-Type", "application/json"),new URLRequestHeader("x-api-user", "67a21444-29bb-406d-ae55-ec8b8cc42736"),new URLRequestHeader("x-api-key", "858f3d44-5573-4175-bd41-1abf746d977c") ];
request.data = JsonObj;
request.method = URLRequestMethod.POST;
try
{
JSONLoader.load(request);
}
catch (error:ArgumentError)
{
trace("An ArgumentError has occurred."+error.errorID.toString());
}
catch (error:SecurityError)
{
trace("A SecurityError has occurred.");
}
catch (error:Error)
{
trace("Unable to load requested document.");
}
}
private function postSuccess(e:Event):void
{
trace ("SuccessfulPOST!");
var myResults = JSON.stringify(e.target.data);
trace ("JSON result is " + myResults);
}
which traces:
SuccessfulPOST!
JSON result is “[{“text”:“from the as3 api!”,“type”:“todo”}]”
It seems these backslashes are being inserted and this is why nothing is being posted, how do I fix this?
Thanks,
Mark