Skip to content

Instantly share code, notes, and snippets.

@dmcblue
Last active February 2, 2019 01:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmcblue/ad4d32cb1bfd9f20ddf9435f57161e6d to your computer and use it in GitHub Desktop.
Save dmcblue/ad4d32cb1bfd9f20ddf9435f57161e6d to your computer and use it in GitHub Desktop.
Make a PUT Request with Haxe
package test;
import haxe.Http;
import haxe.Json;
import haxe.io.BytesOutput;
class PutRequest {
static function main() {
var url = "http://some.url/endpoint";
var data = {
data: 'some data'
};
var response = PutRequest.request(url, data);
trace(response);
}
static public function request(url:String, data:Any) {
var req:Http = new Http(url);
var responseBytes = new BytesOutput();
// Serialize your data with your prefered method
req.setPostData(Json.stringify(data));
req.addHeader("Content-type", "application/json");
req.onError = function(error:String) {
throw error;
};
// Http#onData() is not called with custom requests like PUT
req.onStatus = function(status:Int) {
// For development, you may not need to set Http#onStatus unless you are watching for specific status codes
trace(status);
};
// Http#request is only for POST and GET
// req.request(true);
req.customRequest( true, responseBytes, "PUT" );
// 'responseBytes.getBytes()' must be outside the onStatus function and can only be called once
var response = responseBytes.getBytes();
// Deserialize in kind
return Json.parse(response.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment