1

I am trying to send POST request with custom headers using flash, but I can only get it to send the request as GET even when I use request.method = URLRequestMethod.POST;

Here is my code:

package {
import flash.display.Sprite;
import flash.events.*;
import flash.net.*;

public class URLRequestExample extends Sprite {
    private var loader:URLLoader;

    public function URLRequestExample() {
        loader = new URLLoader();
        configureListeners(loader);
        var url:String = "http://example.com/";
        var headers:Array = [
            new URLRequestHeader("X-CUSTOM-HEADER", "X-VALUE"),
            new URLRequestHeader("Accept", "application/json")
        ];
        var requestVars:URLVariables = new URLVariables();
        requestVars.test = "test";
        request.data = requestVars; // If this line and the one under it are commented the request goes as GET
        request.requestHeaders = headers; // ^with no request headers
        var request:URLRequest = new URLRequest();
        request.method = URLRequestMethod.POST; // Using URLRequestMethod.POST but the request is still sent as GET
        request.url = url;
        try {
            loader.load(request);
        } catch (error:Error) {
            trace("Unable to load requested document.");
        }
    }

    private 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);
    }

    private function completeHandler(event:Event):void {
        var loader:URLLoader = URLLoader(event.target);
        trace("completeHandler: " + loader.data);
    }

    private function openHandler(event:Event):void {
        trace("openHandler: " + event);
    }

    private function progressHandler(event:ProgressEvent):void {
        trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
    }

    private function securityErrorHandler(event:SecurityErrorEvent):void {
        trace("securityErrorHandler: " + event);
    }

    private function httpStatusHandler(event:HTTPStatusEvent):void {
        trace("httpStatusHandler: " + event);
    }

    private function ioErrorHandler(event:IOErrorEvent):void {
        trace("ioErrorHandler: " + event);
    }
}
}

And in my crossdomain.xml file I have:

<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.adobe.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
    <site-control permitted-cross-domain-policies="all"/>
    <allow-access-from domain="*" secure="false"/>
    <allow-http-request-headers-from domain="*" headers="*" secure="false"/>
</cross-domain-policy>
user00239123
  • 270
  • 4
  • 16
  • 1. How do you know it is sent as GET? 2. Where did you put your crossdomain.xml? Normally you don't need it if you run your tests from Flash IDE (or you can allow your working folder in global settings). – Organis Jan 26 '17 at 12:42
  • I am using `http://wonderfl.net/` for live coding (runs directly on the browser). I have my crossdomain.xml file in the root directory. Here is my code: http://wonderfl.net/c/ozHfE if you want to check yourself – user00239123 Jan 26 '17 at 12:47

1 Answers1

1

It seems it handles request as GET if there's no valid POST data attached. As soon as I added at least one valid key:value pair to the URLVariables object my Chrome dev tool reported the request method as POST: http://wonderfl.net/c/Ia2z

Organis
  • 7,243
  • 2
  • 12
  • 14