Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python send image in post request with json data

import base64
import json                    

import requests

api = 'http://localhost:8080/test'
image_file = 'sample_image.png'

with open(image_file, "rb") as f:
    im_bytes = f.read()        
im_b64 = base64.b64encode(im_bytes).decode("utf8")

headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
  
payload = json.dumps({"image": im_b64, "other_key": "value"})
response = requests.post(api, data=payload, headers=headers)
try:
    data = response.json()     
    print(data)                
except requests.exceptions.RequestException:
    print(response.text)
Comment

PREVIOUS NEXT
Code Example
Python :: creating base models django 
Python :: Make a basic pygame window 
Python :: Send GIF in Embed discord.py 
Python :: standardise columns python 
Python :: alphabet python 
Python :: update queryset in django 
Python :: create dictionary from keys and values python 
Python :: get input from user in python 
Python :: pandas remove outliers for multiple columns 
Python :: scroll down selenium python 
Python :: time py 
Python :: basic pygame window 
Python :: convert a pdf folder to excell pandas 
Python :: python Decompress gzip File 
Python :: plot background color matplotlib 
Python :: nn.dropout 
Python :: urllib.request.urlretrieve 
Python :: conda environment 
Python :: esp8266 micropython ds18b20 
Python :: how to append leading zeros in python 
Python :: get file arg0 python 
Python :: remove index in pd.read 
Python :: jupyter notebook for pdf generation 
Python :: change xlabel rotate in seaborn 
Python :: colab version python 
Python :: Using Python Permutations function on a String 
Python :: seaborn boxplot 
Python :: pandas rename column values dictionary 
Python :: python regex 
Python :: count list python 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =