Making requests to https

Hello all.

I have searched for similar topics, but I haven’t found an answer that solves my problem. I am trying to make GET requests with https work in Haxe. I have this code:

class HttpsTest {
    public static function main() : Void {
        trace(haxe.Http.requestUrl("https://api.github.com"));
    }
}

If I run it with haxe -main HttpsTest --interp, it gives the error “Https is only supported with -lib hxssl”.

I checked for the hxssl lib, and on their project page I found this:

Please note hxssl is depreciated from Haxe 3.3.0 on as it has been merged into Haxe core. This library is no longer maintained, but it will still work for previous versions of Haxe.

I am on Haxe 3.4.7, so according to this hxssl should not be neccesary, but Ok, I have installed it and tried again with haxe -main HttpsTest -lib hxssl --interp. Now, the error is “You cannot use the library ‘hxssl’ inside a macro”.

By searching on the web, I found this:

It is fine, since I was just using --interp for testing purporses. Although it says that support is coming on 4.1, I should be able to use https on other targets on 3.4.7, since the message on the hxssl page says that it is integrated into Haxe since 3.3.0.

So let’s try Python (without hxssl since it’s supposed to not be needed anymore):

haxe -main HttpsTest -python output.py

When I run the script with Python3, I get an error:

Traceback (most recent call last):
  File "output.py", line 1812, in <module>
    HttpsTest.main()
  File "output.py", line 81, in main
    print(str(haxe_Http.requestUrl("https://api.github.com")))
  File "output.py", line 760, in requestUrl
    h.request(False)
  File "output.py", line 306, in request
    self.customRequest(post,output)
  File "output.py", line 323, in customRequest
    raise _HxException("Https is only supported with -lib hxssl")
__main__._HxException: Https is only supported with -lib hxssl

So maybe hxssl is not integrated, so I will try by adding it:

haxe -main HttpsTest -lib hxssl -python output.py

But it looks like hxssl does not compile:

/home/javi/haxelib/hxssl/3,0,0-alpha/sys/ssl/Socket.hx:335: characters 9-12 : Type not found : Lib
/home/javi/haxelib/hxssl/3,0,0-alpha/sys/ssl/Socket.hx:334: lines 334-336 : Missing return: Dynamic
/home/javi/haxelib/hxssl/3,0,0-alpha/sys/ssl/Socket.hx:338: characters 33-36 : Type not found : Lib

Sorry if I am missing something obvious here, I am very new to Haxe, but this is driving me crazy.

Thank you for your help!

Uhmm this actually seems to be related to Python target. I have tried Java and PHP (without hxssl) and it works on both. Is support for Python target planned? While I can work with PHP now, I actually will need Python in the final version.

You can try using tink_http GitHub - haxetink/tink_http: Cross platform HTTP abstraction

tink.http.Client.fetch('https://api.github.com').all()
  .handle(function(o) switch o {
    case Success(res): trace(res.body.toBytes());
    case Failure(e): trace(e);
  });

HTTPS is not supported on eval, but you can make the request through CURL
tink.http.Client.fetch('https://api.github.com', {client: Curl})

Thanks for the suggestion, but when compiling to Python it gives me the following error on 3.4.7:

/home/javi/haxelib/tink_http/0,8,2/src/tink/http/clients/SocketClient.hx:48: characters 29-49 : Type not found : python.net.SslSocket
/home/javi/haxelib/tink_http/0,8,2/src/tink/http/clients/SocketClient.hx:53: characters 10-30 : sys.net.Socket should be Void
/home/javi/haxelib/tink_http/0,8,2/src/tink/http/clients/SocketClient.hx:61: characters 8-22 : Void has no field connect
/home/javi/haxelib/tink_http/0,8,2/src/tink/http/clients/SocketClient.hx:64: characters 65-78 : Void has no field output
/home/javi/haxelib/tink_http/0,8,2/src/tink/http/clients/SocketClient.hx:65: characters 71-83 : Void has no field input

You need to use Haxe 4 for SSL support on python target.

Possible alternative:

        var http = new haxe.Http("https://api.github.com");

        http.onData = function (data:String) {
          var result = haxe.Json.parse(data);
          trace(${result});
        }
        
        http.onError = function (error) {
          trace('error: $error');
        }
        
        http.request();

see Loading a file from web - Beginner - Haxe programming language cookbook