POST variously with Python and receive with Flask

I wanted to POST json and images with python, so I summarized them all. I receive them all in Flask.

First of all, POST data normally

post.py


import requests
import json

post_url = "http://127.0.0.1:5000/callback"

#Data you want to post
data = "wowwowwowwow"

#POST transmission
response = requests.post(
                    post_url,
                    data = data
                    )

print(response.json())

server.py


from flask import *
import os
from PIL import Image
import json

app=Flask(__name__)

@app.route("/")
def hello():
    return "hello"

@app.route("/callback",methods=["POST"]) 
def callback():
    print(request.data.decode())
    return jsonify({"kekka": "I received it!"})

if __name__=="__main__":
    port=int(os.getenv("PORT",5000))
    app.debug=True
    app.run()

POST in JSON format

post.py


import requests
import json

post_url = "http://127.0.0.1:5000/callback"
                
json = {"data": "Woooooo"}

#POST transmission
response = requests.post(
                    post_url,
                    json = json,
                    )

print(response.json())

server.py


from flask import *
import os
from PIL import Image
import json

app=Flask(__name__)

@app.route("/")
def hello():
    return "hello"

@app.route("/callback",methods=["POST"]) 
def callback():
    data = request.data.decode('utf-8')#Decode
    data = json.loads(data)
    print(data["data"])
    return jsonify({"kekka": "I received it!"})

if __name__=="__main__":
    port=int(os.getenv("PORT",5000))
    app.debug=True
    app.run()

POST image

post.py


import requests
import json

post_url = "http://127.0.0.1:5000/callback"
                
#Read the file to POST
files = { "image_file": open('./sample.jpg', 'rb') }

#POST transmission
response = requests.post(
                    post_url,
                    files = files,
                    )

print(response.json())

server.py


from flask import *
import os
from PIL import Image
import json

app=Flask(__name__)

@app.route("/")
def hello():
    return "hello"

@app.route("/callback",methods=["POST"]) 
def callback():
    #Loading images
    im = Image.open(request.files["image_file"])

    #display
    im.show()
    
    return jsonify({"kekka": "I received it!"})

if __name__=="__main__":
    port=int(os.getenv("PORT",5000))
    app.debug=True
    app.run()

Impressions

You are also a POST master.

Recommended Posts

POST variously with Python and receive with Flask
POST the image with json and receive it with flask
Programming with Python Flask
Launch a web server with Python and Flask
POST JSON in Python and receive it in PHP
Programming with Python and Tkinter
Encryption and decryption with Python
Python and hardware-Using RS232C with Python-
Install Python and Flask (Windows 10)
POST json with Python3 script
Parse and visualize JSON (Web application ⑤ with Python + Flask)
Send and receive Flask images
python with pyenv and venv
Web application with Python + Flask ② ③
Web application with Python + Flask ④
Works with Python and R
Compare HTTP GET / POST with cURL (command) and Python (programming)
Easy deep learning web app with NNC and Python + Flask
Communicate with FX-5204PS with Python and PyUSB
Shining life with Python and OpenCV
Robot running with Arduino and python
Install Python 2.7.9 and Python 3.4.x with pip.
Neural network with OpenCV 3 and Python 3
AM modulation and demodulation with python
[Python] font family and font with matplotlib
Scraping with Node, Ruby and Python
Scraping with Python, Selenium and Chromedriver
Scraping with Python and Beautiful Soup
[Python] POST wav files with requests [POST]
[Python] Use Basic/Digest authentication with Flask
Quine Post with Qiita API (Python)
JSON encoding and decoding with python
Basic authentication and Digest authentication with Flask
Hadoop introduction and MapReduce with Python
[GUI with Python] PyQt5-Drag and drop-
Post multiple Twitter images with python
Reading and writing NetCDF with Python
I played with PyQt5 and Python3
Login with PycURL and receive response
Reading and writing CSV with Python
Multiple integrals with Python and Sympy
Easily post to twitter with Python 3
Coexistence of Python2 and 3 with CircleCI (1.0)
Easy modeling with Blender and Python
Post bulletin board creation with flask
Application development with Docker + Python + Flask
Sugoroku game and addition game with python
FM modulation and demodulation with Python
Build a detonation velocity website with Cloud Run and Python (Flask)
I made a Nyanko tweet form with Python, Flask and Heroku
Communicate between Elixir and Python with gRPC
Data pipeline construction with Python and Luigi
Calculate and display standard weight with python
Receive textual data from mysql with python
Monitor Mojo outages with Python and Skype
FM modulation and demodulation with Python Part 3
[Automation] Manipulate mouse and keyboard with Python
Passwordless authentication with RDS and IAM (Python)
Python installation and package management with pip
Using Python and MeCab with Azure Databricks
Check and receive Serial port in Python (Port check)