Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to attach a file to a POST for Python2? #26

Open
tclancy opened this issue Feb 18, 2019 · 3 comments
Open

How to attach a file to a POST for Python2? #26

tclancy opened this issue Feb 18, 2019 · 3 comments

Comments

@tclancy
Copy link

tclancy commented Feb 18, 2019

My understanding is in order to upload a document I need to provide an attachmentcontents parameter that is a path to a file on disk, but I don't see how the POST method would leverage that to include the file contents. All my attempts get "error": "Unable to attach uploaded document."

@stevoPerisic
Copy link

stevoPerisic commented Feb 27, 2019

Try this: http://docs.python-requests.org/en/latest/user/quickstart/#post-a-multipart-encoded-file

EDIT:
The link above used to work when it was posted. Try this code snippet:

#!venv/bin/python

#import the request library
import requests

#open the test.txt file in binary mode
file = open('test.txt', 'rb')

#provide the URL you want to upload to
url = 'https://example.com/api/files/'

#set the auth token for the POST route, your POST route shoudl be secured 
headers = {'token': 'c203x302s30s03x0322x0320'}

#if you have data to send with the file you add it to the payload here
payload = {'client_id': 1}

#assign a file to the files dictionary
files = {'file': file}

#call the post method of the request and store the response in the r variable
r = requests.post(url, files=files, data=payload, headers=headers)

#if your server sends a response here you decode it to json
json_data = r.json()

# print your response json
print(json_data)

@dhf0820
Copy link

dhf0820 commented Feb 2, 2020

I would like a real answer to this, nota link to an invalid address.

@stevoPerisic
Copy link

Updated my previous answer with a snippet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants