I wanted to POST json and images with python, so I summarized them all. I receive them all in Flask.
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.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.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()
You are also a POST master.
Recommended Posts