Skip to content

Query parameters

Ravi Teja Gudapati edited this page Dec 26, 2017 · 15 revisions

Query parameters are name-value pairs sent as part of the request URL following ? character.

Example:

POST example.com/todo/1234?name=jaguar&message=hello&at=5

Query parameters are often used to send post/put data of the request. For example, HTML forms with simple inputs are usually encoded as query parameters. Query parameters are also used to send extra parameters of the request.

Accessing Query parameters

queryParams method on Context object shall be used to obtain a query parameter by name.

  server.get('/api/quote', (ctx) {
    final int index = int.parse(ctx.queryParams['index']);
    return quotes[index + 1];
  });

DynamicDottableMap

queryParams is of type QueryParams, which is extended from DynamicDottableMap. This offers some nice features like:

  1. Type casting
  2. Accessing Query parameters as properties

Type casting

Query parameters can be converted to one of the supported target types (int, num, double and bool) using one of the get* methods.

  server.get('/api/quote', (ctx) {
    final int index = ctx.queryParams.getInt('index', 1);
    return quotes[index + 1];
  });

Query parameter index is converted to int using int.parse function.

All these methods take an optional defaultValue argument, which is returned if the conversion fails.

Accessing Query parameters as properties

Class QueryParams also supports accessing of query parameters as properties using dot notation.

  server.get('/api/quote', (ctx) {
    final int index = int.parse(ctx.queryParams.index]);
    return quotes[index + 1];
  });

What's next?

In the next article, we will learn how to serve static files using Jaguar.

Basics

Serialization

Forms

Sessions

Authentication

  • Basic authentication
  • Form authentication
  • JSON authentication
  • Authorization
  • OAuth

Database

Security

Real time

  • Server sent events (SSE)
  • Websockets

Deployment

  • systemd
  • Docker
  • AppEngine

API Documentation

Clone this wiki locally