# Requests

## Requests

## Introduction

The Request class is initialized when the server first starts and changes based on every request by the framework. The Request class is loaded into the IOC container so any Service Provider will have access to it. The IOC container allows all parts of the framework to be resolved by the IOC container and auto inject any dependencies they need. Read more about the IOC container in the [Service Container](/v1.3/architectural-concepts/service-container.md) documentation.

### Getting Started

The `Request` class is bound into the IOC container on every request. This takes the WSGI environment variables generated by your WSGI server as a parameter. This is done already for you by Masonite. This `Request` class is initialized inside the `AppProvider` Service Provider. We grab this request object by simply passing in `Request` into the parameters of anything resolved by the Service Container such as middleware, drivers and controller methods like so:

```python
def show(self, Request):
    # Request is the instance of the Request class
    pass
```

#### Helper Function

Masonite ships with a `HelpersProvider` Service Provider which adds several helper functions. One of these helper functions is the `request()` function. This function will return the request object. Because of this, these two pieces of code are identical:

```python
def show(self, Request):
    Request.input('username')
```

```python
def show(self):
    request().input('username')
```

Notice we didn't import anything at the top of our file and also didn't retrieve any objects from the IOC container. Masonite helper functions act just like any other built in Python function. Read more about helper functions in the [Helper Functions](/v1.3/the-basics/helper-functions.md) documentation.

### Usage

The `Request` has several helper methods attached to it in order to interact with various aspects of the request.

In order to get the current request input variables such as the form data during a `POST` request or the query string during a `GET` request looks like:

```python
def show(self, Request):
    Request.input('username')
```

**NOTE: There is no difference between** `GET` **and** `POST` **when it comes to getting input data. They are both retrieved through this** `.input()` **method so there is no need to make a distinction if the request is** `GET` **or** `POST`

#### Method Options

We can get all the request input variables such as input data from a POST form request or GET data from a query string. This will return all the available request input variables for that request as a dictionary.

```python
def show(self, Request):
    return Request.all()
```

To check if some request input data exists:

```python
def show(self, Request):
    return Request.has('variable')
```

To get the request parameter retrieved from the url. This is used to get variables inside: `/dashboard/@firstname` for example.

```python
def show(self, Request):
    return Request.param('firstname')
```

You may also set a cookie in the browser. The below code will set a cookie named `key` to the value of `value`

```python
def show(self, Request):
    return request.cookie('key', 'value')
```

You can get all the cookies set from the browser

```python
def show(self, Request):
    return request.get_cookies()
```

You can get a specific cookie set from the browser

```python
def show(self, Request):
    return Request.get_cookie('key')
```

Get the current user from the request. This requires the `LoadUserMiddleware` middleware which is in Masonite by default but commented out. This middleware should only be used if you have a valid database connection. This will return an instance of the current user.

```python
def show(self, Request):
    return Request.user()
```

You can specify a url to redirect to

```python
def show(self, Request):
    return Request.redirect('/home')
```

If the url contains `http` than the route will redirect to the external website

```python
def show(self, Request):
    return Request.redirect('http://google.com')
```

You can redirect to a named route

```python
def show(self, Request):
    return Request.redirectTo('dashboard')
```

You can also go back to a named route specified from the form input `back`. This will get the request input named `back` and redirect to that named route. This is great if you want to redirect the user to a login page and then back to where they came from. Just remember during your form submission that you need to supply a `back` input.

```python
def show(self, Request):
    return Request.back()
```

This is equivalent to:

```python
def show(self, Request):
    return Request.redirectTo(request.input('back'))
```

You can also specify the input parameter that contains the named route

```python
def show(self, Request):
    return Request.back('redirect')
```

Sometimes your routes may require parameters passed to it such as redirecting to a route that has a url like: `/url/@firstname:string/@lastname:string`. For this you can use the `send` method. Currently this only works with named routes.

```python
def show(self, Request):
    return Request.back().send({'firstname': 'Joseph', 'lastname': 'Mancuso'})

    return Request.redirectTo('dashboard').send({'firstname': 'Joseph', 'lastname': 'Mancuso'})
```

You can load a specific secret key into the request by using:

```python
Request.key(key)
```

This will load a secret key into the request which will be used for encryptions purposes throughout your Masonite project. **Note that by default, the secret key is pulled from your configuration file so you do NOT need to supply a secret key, but the option is there if you need to change it**


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.masoniteproject.com/v1.3/the-basics/requests.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
