Assuming that you build a frontend of your application with the use of OpenFl (HTML5 target) and would like to communicate it with a backend written in Python (not OpenFl).
What is the proper method for connecting to the external python files from Haxe?
I found only “socket connections”, but using sockets only for sending data between files on the same hosting directory would be a disaster. I hope there is some better solution.
I used to use URLRequest with php files in AS3 era. Because my web applications are getting more and more complicated, I switched from PHP to Python in favour of coding speed and security.
Fortunately I managed to use haxe http request also for python purposes via json. Requests created in this way can be easily received by python web frameworks.
So here is my code which works fine for python:
import haxe.Http;
import haxe.Json;
var string:String = '{"my_var": "my_value"}';
var object:Dynamic = Json.parse(string);
var req = new Http( "http://localhost:5050/my_python_function" );
req.setHeader ("Content-type", "application/json");
req.setPostData(Json.stringify(object));
req.request( true ); // false=GET, true=POST
req.onData = function(response :String) {
trace(response); // - receive data
};