# Requests/Routing

GET should be idempotent and append things to a querystring POST is for data to be processed

| Code                 | Contains                                                                                                                                    |
| -------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- |
| `request.args`       | the key/value pairs in the **URL query string**                                                                                             |
| `request.form`       | the key/value pairs in the **body**, from a HTML post form, or JavaScript request that isn't JSON encoded                                   |
| `request.values`     | combined `args` and `form`, preferring `args` if keys overlap                                                                               |
| `request.files`      | files in the body, which Flask keeps separate from `form`. HTML forms must use `enctype=multipart/form-data` or files will not be uploaded. |
| `request.data`       | mimetype Flask does not handle as string                                                                                                    |
| `request.get_json()` | Get JSON body from a post request                                                                                                           |

## SIMPLE GET ROUTES

Default methods is just get

```python
@app.route('/users/<userid>', methods = ['GET'])
def api_users(userid):
    users = {'1':'john', '2':'steve', '3':'bill'}

    if userid in users:
        return jsonify({userid:users[userid]})
    else:
        return not_found()
```

## SIMPLE POST REQUESTS

Query String

```python
request.args.get('uid')
```

Data sent

```python
from flask import json

@app.route('/messages', methods = ['POST'])
def api_message():
	if not request.is_json:
  	return jsonify({"msg": "Missing JSON in request"}), 400
  
  token = request.json.get('token', None)
```

## Combo

```python
@app.route('/login', methods=['POST', 'GET'])
def login():
    error = None
    if request.method == 'POST':
        if valid_login(request.form['username'],
                       request.form['password']):
            return log_the_user_in(request.form['username'])
        else:
            error = 'Invalid username/password'
    # the code below is executed if the request method
    # was GET or the credentials were invalid
    return render_template('login.html', error=error)
```


---

# 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://openai.gitbook.io/code-cheatsheets/all/python/flask/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.
