Reputation: 41
session = requests.Session()
url = "...api/auth/login"
payload = {
"userName": "[email protected]",
"password": "password"
}
headers = {'content-type': 'application/json'}
response = session.post(url, data=json.dumps(payload), headers=headers)
png_uri = DataURI.from_file(image_path)
files = {
'file': (
os.path.basename(image_path),
png_uri,
'image/png'
),
'filename': os.path.basename(image_path),
'upload': '',
}
response = session.post(upload_url, files=files)
print(response.text)
As shown in the code above I open the image and convert it to dataURI that I send to the endpoint via a POST request.
The issue is that I'm getting an error:
{"error code":400,"message":"Invalid file type"}
Any idea what am I missing here?
Upvotes: 0
Views: 444
Reputation: 41
Apparently, it was much easier than that.
The API was just badly documented...
files = {
'upload': (
os.path.basename(image_path),
image_fp,
'image/png',
)
}
Upvotes: 0
Reputation: 129
So Im not sure how the api you are posting to handles files.
but I created a very simple post to webhook.site so you can see the json data you are sending
I think what is going wrong with your post is this part. The Api that receives the data might not be able to parse the 'file' data.
files = {
'file': (
os.path.basename(image_path),
png_uri,
'image/png'
),
'filename': os.path.basename(image_path),
'upload': '',
}
So instead maybe lay it out like this:
files = {
'file': png_uri,
'type': 'image/png',
'filename': os.path.basename(image_path),
'upload': ''
}
Here is the the difference in json to an api: Your post:
Upvotes: 1