Skip to content

Instantly share code, notes, and snippets.

@dmcblue
Created February 2, 2019 19:30
Show Gist options
  • Save dmcblue/22f98fe04f6bde79d8e63bde55d523ed to your computer and use it in GitHub Desktop.
Save dmcblue/22f98fe04f6bde79d8e63bde55d523ed to your computer and use it in GitHub Desktop.
How to make a PATCH Request in Haxe
package test;
import haxe.Http;
import haxe.Json;
import haxe.io.BytesOutput;
class PatchRequest {
static function main() {
var url = "http://some.url/endpoint";
var data = {
data: 'some data'
};
var response = PatchRequest.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, "PATCH" );
// '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