Create table
CREATE TABLE useraccount (
id serial PRIMARY KEY,
username VARCHAR ( 100 ) NOT NULL,
password VARCHAR ( 100 ) NOT NULL
);
Insert data
INSERT INTO useraccount (username, password) VALUES ('tutorial101', 'pbkdf2:sha256:150000$KxxiGerN$4c37a656baa0034035a6be2cd698b5da8b036ae63eef3ab0b08b9c18b9765648');
Testing Rest API
REST API Testing is open-source web automation testing technique that is used for testing RESTful APIs for web applications. The purpose of rest api testing is to record the response of rest api by sending various HTTP/S requests to check if rest api is working fine or not. Rest api testing is done by GET, POST, PUT and DELETE methods.
Rest stands for Representational State Transfer. It is an architectural style and an approach for communication used in the development of Web Services. REST has become a logical choice for building APIs. It enables users to connect and interact with cloud services efficiently.
An API or Application Programming Interface is a set of programming instructions for accessing a web-based software application.
API is a set of commands used by an individual program to communicate with one another directly and use each other's functions to get information.
Install the Advanced Rest Client
1. Go to Google Chrome's Web Store
2. Search for "Advanced Rest Client" https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo and Install the extension
install psycopg2 https://pypi.org/project/psycopg2/
Psycopg is the most popular PostgreSQL database adapter for the Python programming language.
(venv) PS C:\flaskmyproject> pip install psycopg2
#app.py from flask import Flask, jsonify, request from werkzeug.security import generate_password_hash, check_password_hash import psycopg2 #pip install psycopg2 import psycopg2.extras app = Flask(__name__) DB_HOST = "localhost" DB_NAME = "sampledb" DB_USER = "postgres" DB_PASS = "admin" conn = psycopg2.connect(dbname=DB_NAME, user=DB_USER, password=DB_PASS, host=DB_HOST) @app.route('/') def home(): passhash = generate_password_hash('cairocoders') print(passhash) return passhash @app.route('/user') def get_user(): try: id = request.args.get('id') if id: cursor = conn.cursor(cursor_factory=psycopg2.extras.DictCursor) cursor.execute("SELECT * FROM useraccount WHERE id=%s", id) row = cursor.fetchone() resp = jsonify(row) resp.status_code = 200 return resp else: resp = jsonify('User "id" not found in query string') resp.status_code = 500 return resp except Exception as e: print(e) finally: cursor.close() conn.close() if __name__ == "__main__": app.run()