jaguar_example_json 2.1.1 copy "jaguar_example_json: ^2.1.1" to clipboard
jaguar_example_json: ^2.1.1 copied to clipboard

outdatedDart 1 only

Example to demonstrate how to read JSON requests and write JSON responses using Jaguar web framework.

json #

Examples to demonstrate how to implement JSON routes using Jaguar web framework.

Parsing JSON request #

Context object provides bodyAsJson, bodyAsJsonMap and bodyAsJsonList methods to deserialize JSON requests into built-in Dart objects. convert parameters of bodyAsJson and bodyAsJsonList can be further used to convert built-in Dart object into desired Dart object.

  server.post('/addition', (Context ctx) async {
    AddInput input = await ctx.bodyAsJson(convert: AddInput.fromMap);
    return input.a + input.b;
  });

Writing JSON response #

JSON variants of HTTP methods like getJson, postJson, putJson and deleteJson automatically serializes the returned result into JSON.

  server.postJson('/all', (Context ctx) async {
    AddInput inp = await ctx.bodyAsJson(convert: AddInput.fromMap);
    return AllResults(addition: inp.a + inp.b, subtraction: inp.a - inp.b);
  });

In Controllers use GetJson, PostJson, DeleteJson and PutJson annotations instead.

Client #

The example also includes a client to test run the server.

$> dart bin/client.dart
25
{addition: 25, subtraction: 5}

Postman collection #

The example also includes a Postman collection to test run the server.

0
likes
0
pub points
0%
popularity

Publisher

unverified uploader

Example to demonstrate how to read JSON requests and write JSON responses using Jaguar web framework.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

http, jaguar, jaguar_resty

More

Packages that depend on jaguar_example_json