tags:

views:

289

answers:

2

can anyboy tell me the equivalent of this in flex

curl --insecure --request POST --basic -u [email protected]: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("[email protected]" + ":" + password); myservice.headers["Authorization"] ="basic "+enc.toString(); myservice.send();

but that too doesn`t work... please help..

A: 

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("[email protected]: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>
  */
 }


}
}
just_a_dude
A: 

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("[email protected]: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); 
    }
fiirhok