From this time, it will be the Web API edition. Until now, we have created Web applications on the assumption that people will browse and operate them with a browser. However, in modern times, use from applications other than browsers, such as smartphone applications, has become common. In addition to the official app, it is conceivable that the function will be open to the public and used by the program. In such a case, the Web API is the interface between the Web application and the program, that is, the interface between the Web application and the program according to certain rules. It is also possible to create a Web application from a client-side application using the Web API even in a normal Web application. In this case, the server-side function and the client-side function are separated to improve the visibility of the program.
Until now, the process of returning html in response to the request has been performed. HTML is generally designed to be displayed on a browser and operated by a person. Therefore, it is possible to use html with WebAPI, but it is often troublesome and inefficient to use it programmatically. Therefore, many [^ 1] Web APIs often use a format called JSON (JavaScript Object Notation). The exact format of JSON is defined in RFC 8259. Basically, you can understand the numbers and strings in Python, bool values (true, false), lists that can contain nulls, and dictionaries. The writing style is almost the same. An example of json is shown below.
{
"id": 3,
"name": "hoge",
"ref": [1, 2, 4],
}
In this example, the id is the key, the numerical value is 3, the character string hoge is assigned to the name, and the array [1, 2, 3] is assigned to the "ref".
To return json with the above contents from Flask, do as follows.
from flask import Flask, jsonify
app = Flask(__name__)
#To be able to use Japanese
app.config['JSON_AS_ASCII'] = False
books = [{'name': 'EffectivePython', 'price': 3315}, {'name': 'Expert Python Programming', 'price': 3960}]
@app.route('/')
def get_json():
data = {
"id": 3,
"name": "hoge",
"ref": [1, 2, 4],
}
return jsonify(data)
if __name__ == '__main__':
app.run()
First of all
app.config['JSON_AS_ASCII'] = False
I have made it possible to use Japanese in JSON (this time it does not matter, but it is better to include it). Next, build the object to be returned.
data = {
"id": 3,
"name": "hoge",
"ref": [1, 2, 4],
}
It uses Python dictionaries and lists.
And this data is converted to json and returned.
return jsonify(data)
Use the jsonify function for this.
To summarize the above, you can express JSON using a normal Python dictionary and list, and actually convert it to JSON using the jsonify function.
Until now, I used to check and debug web pages from a browser. However, when debugging WebAPI, it is convenient to be able to debug from PyCharm etc. Especially for POST, we used to use forms, but in the case of WebAPI, it is difficult to operate from a browser because POST is done directly. PyCharm (Professional version) has a function (HTTP client) to create and send HTTP requests on PyCharm. I will introduce its function. You can probably do it with IDEs other than PyCharm. You can also use a tool called cURL. If you use anything other than PyCharm, please check it out.
PyCharm's HTTP client offers two ways, one is to send an ad hoc HTTP request, and the other is to save the HTTP request in a file and send it. This time, I will introduce how to save a reusable HTTP request to a file.
First, select New from the file menu and click HTTP Request (see the figure below).
Then you will be asked to enter the file name, so enter an appropriate name (or get_test if it is clean).
Since we will send a GET request this time, select "GET Request" from "Add Request" on the upper right (see the figure below).
This will insert the GET request template. Rewrite the inserted template as follows according to this time.
GET http://localhost:5000/
Accept: application/json
###
The meaning of this is first
GET http://localhost:5000/
Indicates that a GET request will be sent to "http: // localhost: 5000 /".
Accept: application/json
Indicates that the content type to send is "application / json" (it's a fixed phrase so you don't have to worry too much).
###
Shows the request delimiter when writing multiple HTTP requests in PyCharm's HTTP Client-specific notation. There are many other things I can write, but that's the minimum. Other functions basically follow the HTTP header syntax. I want you to check each one.
Next, let's actually send this request. First, execute the sample program mentioned earlier. After that, you can send the request by returning to HTTPClient and clicking the green ▶ (shown below) to the left of GET in the editor.
Then the execution result will be displayed below.
So far, we have processed JSON back in response to a GET request. Next, I will introduce the process of receiving the POST request and the json contained in it.
The main program (app.py) is as follows.
from flask import Flask, request, jsonify
app = Flask(__name__)
#To be able to use Japanese
app.config['JSON_AS_ASCII'] = False
books = [{'name': 'EffectivePython', 'price': 3315}, {'name': 'Expert Python Programming', 'price': 3960}]
@app.route('/books', methods=['POST'])
def post_json():
#Receive JSON
json = request.get_json()
#Parse JSON
name = json['name']
price = json['price']
book = {'name': name, 'price': price}
book_id = len(books)
books.append(book)
#Build a return dictionary
book['id'] = book_id
return jsonify(book) #Response JSON
@app.route('/books/<book_id>', methods=['GET'])
def get_json_from_dictionary(book_id):
return jsonify(books[int(book_id)])
if __name__ == '__main__':
app.run()
A sample request is below.
POST http://localhost:5000/books
Content-Type: application/json
{
"name": "hoge",
"price": 1000
}
###
GET http://localhost:5000/books/2
Content-Type: application/json
###
After receiving the POST request, you can convert JSON to Python object by using get_json as below.
request.get_json()
Objects converted by get_json can be treated like Python dictionaries and lists (the part below).
#Parse JSON
name = json['name']
price = json['price']
Also, you can define URLs that have the same name but have different HTTP methods (GET and POST here), which have not been explained so far. Web APIs (especially those called REST APIs) often do this. This has the meaning of clearly indicating that it will be acquired in the case of GET and sent in the case of POST.
In the problem of Part 1, Chapter 5, we considered the inventory management system for goods. I want to implement this function as a Web API. Implement a Web API for adding / deleting / acquiring products. Use DB. This time, because of guidance, there is almost something in common with the problem in Part 1, Chapter 5. You may divert the answer.
-Create a table with (product ID, product name, price) as colum --Implement the product acquisition API (/ api / 1.0 / items / id). Since it is an acquisition, use the GET method. Also, the id is variable. Return the JSON including the product ID, product name, and price. --Implement an additional API for the product (/ api / 1.0 / items). Since it is an addition, use the POST method. After sending the JSON including the product name and price, return the JSON including the product ID, product name and price.
--Create a product modification API (/ api / 1.0 / items / id). Since it is an update, use the PUT method. --Create a product deletion page (/ api / 1.0 / items / id). Since it is a delete, use the DELETE method.
[^ 1]: "Many", not all. In the past, a format called XML, which is close to html, was used. Recently, a protocol called gRPC that enables faster communication has also appeared.
Recommended Posts