can anyboy tell me the equivalent of this in flex
curl --insecure --request POST --basic -u testuser@mydomain.com:password --header "Content-Type:application/xml" --data-binary "@c:\curl\examples\New_Activity.xml" https://beta.12sprints.com/v1/activities
basicaly this ia a api in which i need to send the user 开发者_运维百科credentials and a xml file containing the data(new_activity)
for the credentials i tried to add it as a header authencation and encoding it to base64
var enc:Base64Encoder = new Base64Encoder(); enc.encode("saurav.das@sap.com" + ":" + password); myservice.headers["Authorization"] ="basic "+enc.toString(); myservice.send();
but that too doesn`t work... please help..
just_a_dude has the Authentication part, but the xml in his sample doesn't work with the current 12sprints API, and it shouldn't be Base64 encoded. Here's a modified version of his sample that works (just change the username/password):
// the xml we want to send to the server
var xml:String = "<activity name=\"New activity using cURL\"></activity>"
var bytes:ByteArray = new ByteArray();
bytes.writeUTFBytes(xml);
// encoded credentials
var credentials:Base64Encoder = new Base64Encoder();
credentials.encode("testuser@example.com:pass");
var request:URLRequest = new URLRequest("https://beta.12sprints.com/v1/activities");
request.data = bytes;
request.method = URLRequestMethod.POST;
request.requestHeaders.push(new URLRequestHeader("Authorization", "Basic " + credentials));
request.requestHeaders.push(new URLRequestHeader("Content-Type", "application/xml"));
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, completeHandler);
loader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler);
loader.load(request);
}
protected function completeHandler(event:Event):void {
trace("complete");
}
protected function errorHandler(event:Event):void {
trace("error : ", event);
var loader:URLLoader = event.currentTarget as URLLoader;
trace(loader.data);
}
I'm not sure, but maybe this is the way to go
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.net.URLRequestHeader;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;
import flash.utils.ByteArray;
import mx.graphics.codec.PNGEncoder;
import mx.utils.Base64Encoder;
[SWF(backgroundColor = "0xffffff", width = 500, height = 400)]
public class UploadExample extends Sprite {
public function UploadExample () {
// the xml we want to send to the server
var xml:XML = <data>
<activity>
<title>foo</title>
<description>foo bar rules</description>
<created>2009-12-09 15:14:00</created>
</activity>
</data>;
var bytes:ByteArray = new ByteArray();
bytes.writeUTFBytes(xml);
// encoded data
var data:Base64Encoder = new Base64Encoder();
data.encodeBytes(bytes);
// encoded credentials
var credentials:Base64Encoder = new Base64Encoder();
credentials.encode("testuser@mydomain.com:password");
var request:URLRequest = new URLRequest("https://beta.12sprints.com/v1/activities");
request.data = data;
request.method = URLRequestMethod.POST;
request.requestHeaders.push(new URLRequestHeader("Authorization", "Basic " + credentials));
request.requestHeaders.push(new URLRequestHeader("Content-Type", "application/xml"));
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, completeHandler);
loader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler);
loader.load(request);
}
protected function completeHandler(event:Event):void {
trace("complete");
}
protected function errorHandler(event:Event):void {
trace("error : ", event);
var loader:URLLoader = event.currentTarget as URLLoader;
trace(loader.data);
/* this is what get ...
<?xml version="1.0" encoding="UTF-8"?>
<error>
<http_status>401 Unauthorized</http_status>
<message>Could not authenticate you.</message>
</error>
*/
}
}
}
精彩评论