Skip to content

Instantly share code, notes, and snippets.

@BharatKalluri
Last active December 29, 2021 18:42
Show Gist options
  • Save BharatKalluri/20653ffae2a0d8417fdacfe560d98f74 to your computer and use it in GitHub Desktop.
Save BharatKalluri/20653ffae2a0d8417fdacfe560d98f74 to your computer and use it in GitHub Desktop.
Vala 101
class Vala.Basics : GLib.Object {
public static void post_request() {
var soupSession = new Soup.Session ();
var soupMessage = new Soup.Message("POST", "https://reqres.in/api/users");
var body = "{\"name\":\"morpheus\"}";
soupMessage.set_request(
"application/json",
Soup.MemoryUse.COPY,
body.data
);
soupSession.send_message (soupMessage);
stdout.printf("%s \n", (string) soupMessage.response_body.data);
// {"name":"morpheus","id":"555","createdAt":"2021-12-29T18:15:12.065Z"}
}
public static void parse_json () throws Error {
string sampleJson = "{\"name\":\"morpheus\"}";
var jsonParser = new Json.Parser ();
jsonParser.load_from_data (sampleJson);
var characterName = jsonParser.get_root().get_object().get_string_member("name");
stdout.printf("%s \n", characterName);
// morpheus
}
async static Soup.Message async_post () {
var soupSession = new Soup.Session ();
var soupMessage = new Soup.Message("POST", "https://reqres.in/api/users");
var body = "{\"name\":\"morpheus\"}";
soupMessage.set_request(
"application/json",
Soup.MemoryUse.COPY,
body.data
);
soupSession.send_message (soupMessage);
return soupMessage;
}
// async_post.begin ((obj, res) => {
// var result = async_post.end(res);
// stdout.printf("%s \n", (string) result.response_body.data);
// });
public static int main(string[] args) {
Vala.Basics.parse_json();
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment