Web application production course learned with Flask of Python Part 2 Chapter 1 ~ JSON exchange ~

What is Web API?

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.

What is JSON

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".

Return JSON

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.

How to debug Web API from PyCharm

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). スクリーンショット 2020-02-12 18.12.58.png

Then you will be asked to enter the file name, so enter an appropriate name (or get_test if it is clean). スクリーンショット 2020-02-12 18.13.36.png

Since we will send a GET request this time, select "GET Request" from "Add Request" on the upper right (see the figure below). スクリーンショット 2020-02-12 18.14.03.png

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. スクリーンショット 2020-02-12 18.15.12.png

Then the execution result will be displayed below.

Receive JSON on POST request

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.

problem

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.

development

--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

Web application production course learned with Flask of Python Part 2 Chapter 1 ~ JSON exchange ~
Web application with Python + Flask ② ③
Web application with Python + Flask ④
Parse and visualize JSON (Web application ⑤ with Python + Flask)
Web application made with Python3.4 + Django (Part.1 Environment construction)
Web application development with Flask
Application development with Docker + Python + Flask
Let's make a WEB application for phone book with flask Part 1
Web application created with Python + Flask (using VScode) # 1-Virtual environment construction-
Python x Flask x PyTorch Easy construction of number recognition web application
Let's make a WEB application for phone book with flask Part 2
Let's make a WEB application for phone book with flask Part 3
Let's make a WEB application for phone book with flask Part 4
Algorithm learned with Python 8th: Evaluation of algorithm
100 Language Processing Knock with Python (Chapter 2, Part 2)
[Python] A quick web application with Bottle!
100 Language Processing Knock with Python (Chapter 2, Part 1)
[GCP] Procedure for creating a web application with Cloud Functions (Python + Flask)
Easy web app with Python + Flask + Heroku
1. Statistics learned with Python 1-3. Calculation of various statistics (statistics)
Run a Python web application with Docker
Data acquisition from analytics API with Google API Client for python Part 2 Web application
[ES Lab] I tried to develop a WEB application with Python and Flask ②
Deep Learning from scratch The theory and implementation of deep learning learned with Python Chapter 3
Algorithm learned with Python 13th: Tower of Hanoi
Vienna with Python + Flask web app on Jenkins
1. Statistics learned with Python 1-2. Calculation of various statistics (Numpy)
Launch a web server with Python and Flask
"Principle of dependency reversal" learned slowly with Python
[Part.3] Crawling with Python! It's JSON rather than CSV! ??
How to upload with Heroku, Flask, Python, Git (Part 3)
Derivatives Learned Using Python-(1) Calculation of Forward Exchange Rate-
[Chapter 5] Introduction to Python with 100 knocks of language processing
Launch a Python web application with Nginx + Gunicorn with Docker
[Chapter 3] Introduction to Python with 100 knocks of language processing
How to upload with Heroku, Flask, Python, Git (Part 1)
How to upload with Heroku, Flask, Python, Git (Part 2)
Creating BINGO "Web Tools" with Python (Table of Contents)
[Chapter 2] Introduction to Python with 100 knocks of language processing
[Basics of Modern Mathematical Statistics with python] Chapter 1: Probability
Hobby Web engineer develops web application with Vue.js + Flask (& GCP)
[Chapter 4] Introduction to Python with 100 knocks of language processing
[Python] Use JSON with Python
Application of Python 3 vars
Programming with Python Flask
Python x Flask x Tensorflow.Keras Web application for predicting cat breeds 2
I made a simple book application with python + Flask ~ Introduction ~
[Part.2] Crawling with Python! Click the web page to move!
[CleanArchitecture with Python] Part2: Frameworks & Drivers Layer: Introducing the Web
Web application with Python3.3.1 + Bottle (1) --Change template engine to jinja2
1. Statistics learned with Python 2. Probability distribution [Thorough understanding of scipy.stats]
Sample of HTTP GET and JSON parsing with python of pepper
Easy deep learning web app with NNC and Python + Flask
Easy-to-understand explanation of Python Web application (Django) even for beginners (5) [Introduction to DB operation with Django shell]
Create a Python3.4 + Nginx + uWSGI + Flask Web application execution environment with haste using pyenv on Ubuntu 12.04