Skip to content

Query parameters

Ravi Teja Gudapati edited this page Jan 9, 2019 · 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

query 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];
  });

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.

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