Created
February 2, 2019 19:30
-
-
Save dmcblue/22f98fe04f6bde79d8e63bde55d523ed to your computer and use it in GitHub Desktop.
How to make a PATCH Request in Haxe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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