Created
May 5, 2017 15:18
-
-
Save alenstarx/ae40d6abb036fa2a1a24a45773f87ee7 to your computer and use it in GitHub Desktop.
POST requests in gjs
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
const Lang = imports.lang; | |
const Soup = imports.gi.Soup; | |
let _session = new Soup.SessionAsync(); | |
function POST(url, params, callback) { | |
let request = Soup.Message.new('POST', url); | |
let _params = Soup.form_encode_hash(params); | |
request.set_request('application/x-www-form-urlencoded', | |
Soup.MemoryUse.COPY, | |
_params, | |
_params.length); | |
_session.queue_message(request, Lang.bind(this, | |
function(session, message) { callback(message.status_code, request.response_body.data); | |
} | |
) | |
); | |
} | |
// Standalone script only section | |
const Mainloop = imports.mainloop; | |
POST('http://example.org/create', {'foo': 'bar'}, function(code, data) { log('post done'); log(code); log(data); Mainloop.quit(true); }); | |
Mainloop.run(true); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment