Coming soon - Get a detailed view of why an account is flagged as spam!
view details

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.

3
Help with taking Werkzeug FileStorage and sending it to REST API
Post Body

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?

Author
Account Strength
90%
Account Age
8 years
Verified Email
Yes
Verified Flair
No
Total Karma
3,303
Link Karma
1,163
Comment Karma
2,140
Profile updated: 3 days ago
Posts updated: 9 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
5 years ago