在as3 AIR中发出HTTP POST请求

时间:2015-04-07 12:08:11

标签: actionscript-3 air httprequest

我正在尝试通过as3 AIR for mobile发出HTTP POST请求,相当于此卷曲请求:

curl -d '{"firstname": "John", "lastname": “Doe"}' -H 'Content-Type: application/json' http://eve-demo.herokuapp.com/people

这是我的代码:

var urlVariables:URLVariables = new URLVariables();
urlVariables.firstname = "Jhon";
urlVariables.lastname = "Doe";

var request:URLRequest = new URLRequest();
request.url = "http://eve-demo.herokuapp.com/people";
request.contentType = "multipart/form-data";
request.method = URLRequestMethod.POST;
request.data = urlVariables;

var contentTypeHeader:URLRequestHeader = new URLRequestHeader("Content-Type", "application/json");
var acceptHeader:URLRequestHeader = new URLRequestHeader("Accept", "application/json");
var formDataHeader:URLRequestHeader = new URLRequestHeader("Content-Type", "multipart/form-data");

request.requestHeaders = [acceptHeader, formDataHeader];

postLoader = new URLLoader();
postLoader.dataFormat = URLLoaderDataFormat.BINARY;
postLoader.addEventListener(Event.COMPLETE, loaderCompleteHandler);
postLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
postLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
postLoader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);

try
{
    postLoader.load(request);
}
catch (error:Error)
{
    trace("Unable to load post URL");
}

但我收到以下错误:

[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: http://eve-demo.herokuapp.com/people" errorID=2032]

任何人都有任何建议如何使用' multiform / form-data'进行http发布请求(没有任何文件上传)??

由于

1 个答案:

答案 0 :(得分:0)

URLVariables旨在与url编码的变量一起使用,而不是JSON。尝试将data设置为JSON字符串:

var params:Object = { firstname: "John", lastname: "Doe" };
request.data = JSON.stringify(params);