suppose I want to post an image file with some metadata to a web service and then save that file to a location somewhere. How do I do it? I have seen examples of posting data and examples of getting data from a request but can't get anything working which does both, which means I can't verify either. So here's one variation of the code I culled from a samples I found here and another here:
import requests
import json
import cv2
import shutil
content_type = 'image/jpeg'
headers = {'content-type': content_type}
addr = 'http://127.0.0.1:5000'
test_url = addr + '/step/Update-Image'
path = 'D:/Temp/TrainingPics'
inFile = "/Drew16.jpg"
outFile = "/Test.jpg"
image_metadata = {'UserId': 'test', 'Type': 'test2'}
data = {'name': 'drew16.jpg', 'data': json.dumps(image_metadata)}
files = {'media': open(path + inFile, 'rb')}
r = requests.post(test_url, files=files, data=image_metadata)
if r.status_code == 200:
with open(path + outFile, 'wb') as f:
r.raw.decode_content = True
shutil.copyfileobj(r.raw, f)
I get a status code 200 but the output file is corrupted and I can't see where to pull the metadata. Where am I going wrong here? Any help would be appreciated.