You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: .learn/exercises/05-returning-json/README.es.md
+12-12Lines changed: 12 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,25 +2,25 @@
2
2
3
3
Las REST APIs tienen que retornar datos en formato JSON, no en formato HTML.
4
4
5
-
Puedes usar la función [flask jsonify](https://flask.palletsprojects.com/en/1.1.x/api/#flask.json.jsonify) para convertir con facilidad cualquier tipo de datos básico a JSON de esta forma:
5
+
Puedes usar la función [flask jsonify](https://flask.palletsprojects.com/en/3.0.x/api/#flask.json.jsonify) para convertir con facilidad cualquier tipo de datos básico a JSON de esta forma:
6
6
7
7
```python
8
-
#añade el método jsonify a tu importación de Flask
9
-
from flask import Flask, jsonify
8
+
#Añade el método jsonify a tu importación de Flask
9
+
from flask import Flask, jsonify
10
10
11
-
#supongamos que tienes tus datos en la variable some_data
#puedes convertir esa variable en una cadena json de la siguiente manera
16
+
#Puedes convertir esa variable en una cadena json de la siguiente manera
17
17
json_text = jsonify(some_data)
18
18
19
-
#y luego puedes devolverlo al front-end en el cuerpo de la respuesta de la siguiente manera
19
+
#Y luego puedes devolverlo al front-end en el cuerpo de la respuesta de la siguiente manera
20
20
return json_text
21
21
```
22
22
23
-
Si aplicamos estos conocimientos a nuestro proyecto de todo-list, podemos crear una variable global `todos` que va a contener la lista de todos de esta forma:
23
+
Si aplicamos estos conocimientos a nuestro proyecto de todo-list, podemos crear una variable global `todos` que va a contener la lista de todos de esta forma:
24
24
25
25
```python
26
26
todos = [
@@ -29,12 +29,12 @@ todos = [
29
29
]
30
30
```
31
31
32
-
## 📝 Instrucciones
32
+
## 📝 Instrucciones:
33
33
34
-
1. Crea una variable global todos, declárala globalmente. No la declares dentro de una función, declárala en cualquier lado pero a nivel global. Asegúrate de que la variable contenga por lo menos una tarea (task) con la siguiente estructura:
34
+
1. Crea una variable global `todos`, declárala globalmente. No la declares dentro de una función, declárala en cualquier lado pero a nivel global. Asegúrate de que la variable contenga por lo menos una tarea (task) con la siguiente estructura:
35
35
36
36
```python
37
-
{ "label": "My first task", "done": False }
37
+
[ { "label": "My first task", "done": False } ]
38
38
```
39
39
40
40
2. Cambia la función del endpoint de tu método GET para que retorne la versión en json (Usando jsonify) de la variable de `todos` recién creada.
Copy file name to clipboardExpand all lines: .learn/exercises/05-returning-json/README.md
+10-11Lines changed: 10 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,26 +2,25 @@
2
2
3
3
REST APIs have to return data in JSON format, not HTML format.
4
4
5
-
You can use the [flask jsonify](https://flask.palletsprojects.com/en/1.1.x/api/#flask.json.jsonify) function to easily convert any of the basic data-types to JSON data like this:
5
+
You can use the [flask jsonify](https://flask.palletsprojects.com/en/3.0.x/api/#flask.json.jsonify) function to easily convert any of the basic data-types to JSON data, like this:
6
6
7
7
```python
8
+
# Add the jsonify method to your Flask import
9
+
from flask import Flask, jsonify
8
10
9
-
# add the jsonify method to your Flask import
10
-
from flask import Flask, jsonify
11
-
12
-
# suppose you have your data in the variable named some_data
#you can convert that variable into a json string like this
16
+
#You can convert that variable into a json string like this
18
17
json_text = jsonify(some_data)
19
18
20
-
#and then you can return it to the front end in the response body like this
19
+
#And then you can return it to the front end in the response body like this
21
20
return json_text
22
21
```
23
22
24
-
If we apply this knowledge to our ToDo-list project, we can create a global variable named `todos` that will hold the list of todos like this:
23
+
If we apply this knowledge to our todo-list project, we can create a global variable named `todos` that will hold the list of todos like this:
25
24
26
25
```python
27
26
todos = [
@@ -30,12 +29,12 @@ todos = [
30
29
]
31
30
```
32
31
33
-
## 📝Instructions:
32
+
## 📝Instructions:
34
33
35
34
1. Create a global variable called `todos`. Do not declare the variable inside any function. Declare the variable in the global scope and make sure the variable contains at least one task item (our task objects) inside with the following structure:
36
35
37
36
```python
38
37
[ { "label": "My first task", "done": False } ]
39
38
```
40
39
41
-
2.Change the function on the GET method's endpoint from string output so that it will return the jsonified version of the global variable `todos`.
40
+
2. Change the function on the GET method's endpoint from string output so that it will return the jsonified version of the global variable `todos`.
Copy file name to clipboardExpand all lines: .learn/exercises/07-post_todo/README.es.md
+9-12Lines changed: 9 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
# `07` POST /todos (añade un nuevo task)
1
+
# `07` POST /todos (add a new task)
2
2
3
3
Ahora que ya está hecho el método GET `/todos`, debemos pensar en el resto de los endpoints de nuestra API:
4
4
@@ -8,26 +8,23 @@ POST /todos
8
8
DELETE /todos
9
9
```
10
10
11
-
Para poder construir el `POST /todos` debemos hacer algo similar a lo que hicimos en el primer endpoint, recuerda que cada endpoint en una Flask API está representada por una función y decorador como este:
11
+
Para poder construir el `POST /todos` debemos hacer algo similar a lo que hicimos en el primer endpoint, recuerda que cada endpoint en una Flask API está representada por una función y un decorador de esta manera:
12
12
13
13
```python
14
-
@app.route('/blabla', methods=['GET'])
14
+
@app.route('/myroute', methods=['GET'])
15
15
defhello_world():
16
-
return'Hello, World!'
16
+
return'Hello World!'
17
17
```
18
18
19
-
Pero en este caso no esperaremos una solicitud `GET`.
19
+
Pero en este caso no esperaremos una solicitud `GET`, sino una solicitud `POST`.
20
20
21
-
También, esperamos recibir el `todo` (tarea) que el cliente quiere añadir dentro del `request_body` (cuerpo de la solicitud), solo que en este caso, no esperaremos una `request`(solicitud) `GET`.
22
-
23
-
Esperamos recibir el `todo` que el cliente desea añadir dentro del `request_body`.
21
+
También, esperamos recibir el TODO que el cliente quiere añadir dentro del `request_body` (cuerpo de la solicitud).
24
22
25
23
```python
26
24
from flask import request
27
25
28
-
# el request_body o cuerpo de la solicitud ya fue decodificado por json y se encuentra en la variable request.data
29
-
30
-
print(request.data)
26
+
# El request_body o cuerpo de la solicitud ya está decodificado en formato JSON y se encuentra en la variable request.json
27
+
print(request.json)
31
28
```
32
29
33
30
## 📝 Instrucciones:
@@ -37,7 +34,7 @@ print(request.data)
37
34
```python
38
35
@app.route('/todos', methods=['POST'])
39
36
defadd_new_todo():
40
-
request_body = request.data
37
+
request_body = request.json
41
38
print("Incoming request with the following body", request_body)
Copy file name to clipboardExpand all lines: .learn/exercises/07-post_todo/README.md
+13-11Lines changed: 13 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,12 +8,12 @@ POST /todos
8
8
DELETE /todos
9
9
```
10
10
11
-
In order to build the `POST /todos` endpoint, we have to do something similar to what we did in the first endpoint with our GET method. Remember that each endpoint in a Flask API is represented by a function ( def my_function(): ) and a decorator ( @app.route() ) like this:
11
+
In order to build the `POST /todos` endpoint, we have to do something similar to what we did in the first endpoint with our GET method. Remember that each endpoint in a Flask API is represented by decorator and a function, like this:
12
12
13
13
```python
14
14
@app.route('/myroute', methods=['GET'])
15
15
defhello_world():
16
-
return'Hello, World!'
16
+
return'Hello World!'
17
17
```
18
18
19
19
In this case, we are not going to be expecting a `GET` request, but rather a `POST` request.
@@ -23,24 +23,26 @@ Also, we are expecting to receive the TODO that the client wants to add inside o
23
23
```python
24
24
from flask import request
25
25
26
-
#the request body is already json decoded and it comes in the request.json variable
26
+
#The request body is already JSON decoded, and it comes in the request.json variable
27
27
print(request.json)
28
28
```
29
29
30
30
## 📝 Instructions:
31
31
32
-
1. Remember to import `request` at the top of the file:
33
-
34
-
```python
35
-
from flask import request
36
-
```
37
-
38
-
2. Then, Add the folowing endpoint to your app.py and test it:
32
+
1. Add the following endpoint to your `app.py` and test it:
39
33
40
34
```python
41
35
@app.route('/todos', methods=['POST'])
42
36
defadd_new_todo():
43
-
request_body = request.data
37
+
request_body = request.json
44
38
print("Incoming request with the following body", request_body)
45
39
return'Response for the POST todo'
46
40
```
41
+
42
+
2. Remember to import `request` at the top of the file:
0 commit comments