How To Run Flask-restful Service In Windows In Pycharm
REST API services let y'all interact with the database past simply doing HTTP requests. In this article you lot learn how to write a Residuum server using the Flask.
This is often how the backend of web apps is created. Returning data is in JSON format and requests we are using are PUT, DELETE, Postal service, and Get
If yous want to put your API online, apply: PythonAnywhere.
Related course: Python Flask: Create Web Apps with Flask
Flask API example
Introduction
To make our first program, recall that we enter the URL in the browser
one | localhost:5000 |
At the time, a cord "Hello Globe!" was returned, so we thought, can we supervene upon this cord with a json sequence? Isn't that the aforementioned as a REST query API?
So, we might have the starting time impulse to do this:
1 |
|
Actually, nosotros just modified the returned string, modified information technology to a string of JSON, and then we opened it on the browser
1 | localhost:5000 |
Wow! It seems to have achieved the function nosotros wanted, returned a JSON cord.
Just we opened the debug tool for Chrome (which I use as a tool like to Chrome, Safari, Firefox) (nether Windows: Ctrl + Alt + I, Mac under: Cmd + Shift + I), we tin can see that this returned information type really is of type html:
You may wonder what bear on this might have, the impact should exist small-scale in most cases, but for some mobile-side libraries, the data may be candy according to the response (incorrectly!).
If you lot desire to put your API online, use: PythonAnywhere.
Return json
To deal with this situation, we can't just prepare this response head into json format.
A better solution is to utilize the jsonify function of the Flask, where I use this part to modify the code:
one |
|
The changes are:
1 | from flask import ...., jsonify |
Look at Google Dev Tools, you'll run into the content-type modify to JSON.
Asking method
We know that there are 6 commonly used HTTP request methods, which are
- GET
- POST
- PUT
- DELETE
- PATCH
- HEAD
The code that we just had had to deal with GET by default (the browser defaults to using Become), so how do you program the other requests?
Like this:
1 | |
The programme below demonstrates this:
1 |
|
The code is long, merely the lawmaking is easier to empathize, and it is a relatively unproblematic file operation.
The code that we need to focus on is the post-obit:
- How to ready request methods
1 | |
- How to get data
If you want to put your API online, use: PythonAnywhere.
How To Run Flask-restful Service In Windows In Pycharm,
Source: https://pythonbasics.org/flask-rest-api/
Posted by: lenzwhas1956.blogspot.com
0 Response to "How To Run Flask-restful Service In Windows In Pycharm"
Post a Comment