REST API Tutorial

Python Requests Post Example

Summary of learning

This tutorial help to create Python HTTP post requests example. The HTTP POST request help to post data to the server in the form of json or form.

This tutorial help to create Python HTTP post requests example. The HTTP POST request help to post data to the server in the form of json or form.

You can create a New or Update an existing 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 requests o post payloads into the server.

How To Use Python HTTP Post Request

We will install the following dependencies using python pip.The pip is the package manager for the python application. First, we will create a ‘sample-api’ folder and change the directory into it.

$sample-api> py -m pip install flask requests flask_cors flask_restful

We will create a api.py file and added the 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 the 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.

If you have requirements that needs our attentioN

Please feel free to contact us

We would love to hear from you

Share with your network

Facebook
Twitter
LinkedIn
WhatsApp
Email

This tutorial is intended to cover all the essentials steps and insights to understand Python Requests Post Example

Genislab Technologies

Learn further