pythonhttpgetglibgobject-introspection

How do you make a HTTP GET request with libSoup and GI?


I want to make a simple HTTP GET request to https://httpbin.org/get with the libSoup GLib library, using GObject-introspection (gi) in python.

How do you do this?


Solution

  • Here's an example that makes the request and prints out the response:

    import sys
    
    import gi
    gi.require_version('Soup', '2.4')
    from gi.repository import Gio, Soup
    
    session = Soup.Session()
    uri = Soup.URI.new('https://httpbin.org/get')
    request = session.request_http_uri('GET', uri)
    stream = request.send(cancellable=None)
    out = Gio.UnixOutputStream(fd=sys.stdout.fileno())
    out.splice(stream, Gio.OutputStreamSpliceFlags.CLOSE_SOURCE, cancellable=None)