Created
May 5, 2017 15:19
-
-
Save alenstarx/26d5bb415d899588238ce46abf2a0183 to your computer and use it in GitHub Desktop.
Post application/json 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 POSTJSON(url, params, callback) { | |
log('post started'); | |
let request = Soup.Message.new('POST', url); | |
log('req created'); | |
let _params = JSON.stringify(params) | |
log('params created'); | |
request.set_request('application/json', | |
Soup.MemoryUse.COPY, | |
_params, | |
_params.length); | |
_session.queue_message(request, Lang.bind(this, | |
function(session, message) { | |
callback(message.status_code, request.response_body.data); | |
} | |
) | |
); | |
log('message qd'); | |
} | |
// Standalone script only section | |
const Mainloop = imports.mainloop; | |
POSTJSON('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