Welcome to the Finch HTTP Requests Guide! This guide will walk you through the steps to handle HTTP requests in your Finch application. Whether you're a seasoned developer or just starting, Finch offers a robust set of tools to simplify server-side web app development.

Request Object

The Request object is used to handle HTTP requests in your Finch application. It contains all the necessary properties to handle a request. Here is an example of how to use it:

app.get(
  path: '/',
  index: (rq) async {
    return rq.renderString(text: 'Hello World');
  },
);

in Finch application Requrst variables named rq. You can use this variable to access all the properties of the Request object.

Properties:

  • httpRequest: The HttpRequest object of the request.
  • session: The HttpSession object of the request.
  • cookies: The list of cookies of the request.
  • stream: The stream of the request.
  • uri: The URI of the request.
  • response: The response of the request.
  • method: The method of the request.
  • isPost: Check if the request is a POST request.
  • etc. that you can find in the Request class.

Reading Request Data

Finch provides a convenient way to read request data. You can use the data method of the Request object to read request data. Here is an example of how to use it:

here is the list of methods to read request data:

  • get<T>(key, {def, trim = true}): Read data from the request. It is a generic method that can read any type of data.

this kind if data can be read from the request of GET and POST requests:

app.get(
  path: '/',
  index: (rq) async {
    var name = rq.get<String>('name', def: 'World');
    return rq.renderString(text: 'Hello $name');
  },
);

parameters

parameters are data that are sent in the URL or while processing the request you can add them to the request using addParam method. You can read them using getParam method. For example, if you have a URL like this: /users/{id} you can read the id parameter using getParam method.

app.get(
  path: '/users/{id}',
  index: (rq) async {
    var id = rq.getParam('id');
    return rq.renderString(text: 'User ID: $id');
  },
);

set parameters

You can set parameters using the addParam method. This is useful when you want to add some data to the request object. For example, you can add the user object to the request object after authentication.

app.get(
  path: '/users/{id}',
  index: (rq) async {
    var id = rq.getParam('id');
    var user = await User.findById(id);
    rq.addParam('user', user);
    return rq.renderString(text: 'User ID: $id');
  },
);

Note:

by using addParam method you can add any type of data to the request object. also you can use them in your templates. for example, you can add the user object to the request object and use it in your template like this: {{ user.name }}.

the future parameters are not allowed in the template. so you can wait for the data using async and await keywords. for example:

app.get(
  path: '/users/{id}',
  index: (rq) async {
    var id = rq.getParam('id');
    var user = await User.findById(id);
    rq.addParam('user', user);
    return rq.renderString(text: 'User ID: $id');
  },
);