This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
Hi r/learnpython !
I'm currently building small flask app that takes a filled form with image attachments, and posts it to a REST API as multipart/form-data.
I've managed to make this work with just text fields posting to the endpoint, but now I am trying to add images, and am a bit stuck.
The file attachments come in as a FileStorage werkzeug data structure ( https://kite.com/python/docs/werkzeug.FileStorage )
def put_defect(access_token, files, defectname):
post_data = {'project_id': 'xxxx',
'punch_item': {'name': defectname}}
i = 0
post_img = {}
for img in files:
post_img['attachment_item[i]'] =
img.read
()
i = 1
post_data['attachments'] = post_img
headers = base_headers()
headers.update({"Authorization": "Bearer " access_token})
response = requests.post("https://sandbox.procore.com/vapid/punch_items", headers=headers, json=post_data)
me_json = response.json()
return me_json
In case you want a better formatted paste: https://pastebin.com/872ak2jB
For a point of reference, if I make the above post, but don't include post_data['attachments']
in the post data, it works.
The documentation says I am meant to send the attachments
Here is the relevant doc for the /vapid/punch_items action: https://developers.procore.com/reference/punch-items#create-punch-item
Here is a doc that explains how to do it in Postman: https://developers.procore.com/documentation/attachments
But I just can't wrap my head around how to do it in Python!
With the code above, I get
TypeError: Object of type bytes is not JSON serializable
Can someone please push me in the right direction?
Subreddit
Post Details
- Posted
- 5 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/learnpython...