Skip to content

Instantly share code, notes, and snippets.

@alenstarx
Created May 5, 2017 15:18
Show Gist options
  • Save alenstarx/ae40d6abb036fa2a1a24a45773f87ee7 to your computer and use it in GitHub Desktop.
Save alenstarx/ae40d6abb036fa2a1a24a45773f87ee7 to your computer and use it in GitHub Desktop.
POST requests in gjs
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