Largely based on this Udemy course
- Sometimes referred to the hardware
- Sometimes referred to a piece of software designed to respond to a user request
A request is data that a user/web app sends to a web server
GET /HTTP/1.1
Host www.google.com
- Error
- HTML code
- Some processed output
- Nothing
- If it does not support HTTP (eg: an SMTP server)
- If path not found (404)
- If server is unavailable
When you access https://twitter.com/login
GET /login HTTP/1.1
Host: https://twitter.com
Verb | Meaning | Example |
---|---|---|
GET | (User) Retrieve something | GET /login |
POST | (Server) Receive data and use it | POST /item {'name':'chair','price':9.99} |
PUT | Makes sure that there is something. If there isn't anything it will create | PUT /item {'name':'chair','price':9.99} |
DELETE | Remove something | DELETE /item/1 |
- Its a way of thinking/designing how a web server responds to a user's requests
- Two main characteristics:
- Web server doesn't respond with data, responds with resources instead
- Interactions are stateless
Resources are basically abstractions that encapsulate data. Its a way of thinking about data and how the server and user use it for interaction. GET /item/chair
is retrieving the chair resource
- A user logs into web application
- Server does not keep track of requests and responses related to the login and thus does not know that the user is logged in
- The web application must send enough data to identify the user in every request or else the server won't associate the request with the user
- In the same way, the server must be programmed to associate every request to the specific user
from flask import Flask
app = Flask(__name__)
@app.route('/store',methods=['POST'])
def some_function():
pass
from flask import Flask
app = Flask(__name__)
@app.route('/store') #default is GET
def some_function():
pass
from flask import Flask
app = Flask(__name__)
@app.route('/store/<string: name>/item',methods=['POST'])
def some_function(name):
pass
jsonify is required to convert Python dictionary into a json object that can be read by JavaScript
from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/store/<string: name>/item',methods=['POST'])
def some_function(name):
some_python_dicitonary = {....}
return jsonify(some_python_dicitonary)
from flask import Flask, jsonify, request
app = Flask(__name__)
old_data =[{...},{...},{...}]
@app.route('/store/<string: name>/item',methods=['POST'])
def some_function(name):
request_data = request.get_json() #converts json to dictionary
new_data = {
'key1':request_data['key1']
'key2':request_data['key2']
}
old_data.ppend(new_data)
return jsonify(new_data)