This tutorial help to create Python HTTP post requests example.The HTTP POST request help to post data to the server in json or form based.
You can create new or add record using Python Http Post request.
I am using python 3.4+ and flask framework to create http post call.I am using some dependencies to create post request o post payload into server.
How To Use Python HTTP Post Request
We will install following dependencies using pip.The pip is the package manager for python application.First, we will create 'sample-api'
folder and change directory into it.
$sample-api> py -m pip install flask requests flask_cors flask_restful
We will create api.py
file and added below code into this file.
from flask import Flask from flask_restful import Resource, Api from flask_cors import CORS import requests from flask import request import json app = Flask(__name__) CORS(app) ## To allow direct AJAX calls @app.route('/blog_post', methods=['POST']) def create(): """ data = {'title': 'parvez', 'body': 'adam', 'userId': 1}""" print(request.get_data('title')) r = requests.post('https://jsonplaceholder.typicode.com/posts', json = request.data) return r.json() if __name__ == '__main__': app.run(debug = True)
I am passing following data as json payloads using postman, and will get into '/blog_post'
route path.
We ware using json = request.data
for json type payload, For form-data
then we will use data = request.data
.
Since, I am passing data using postman, So i am getting data using request.data, Otherwise pass direct dictionary like data = payloads
.
The post Python Requests Post Example appeared first on Rest Api Example.