1

I am trying to represent the following curl statement in python:

curl --data-binary @sample.png --data project = 23423233 -H 'X-API-KEY: YOUR API KEY, User-Agent: AppName ([email protected])' https://files.proofhub.com/files/upload

I have already done multiple post & get requests, however since this one uses the data option, I cannot get my head around how I would execute this using requests.

I will post my current code:

data = open(r"C:\Users\dlogan.CLEARDATA\Desktop\ProofHub Upload\test.txt",'rb')

create_headers = {'X-API-KEY': '', 'Content-Type': 'application/json', 'User-Agent': '@cleardata.co.uk'}

r = requests.post('https://cleardata.proofhub.com/files/upload', data=data, headers=create_headers)

Does anyone know how i'd go about including an file?

4
  • Have you tried just reading the file? data = open(r"C:\Users\dlogan.CLEARDATA\Desktop\ProofHub Upload\test.txt",'rb').read() Commented Apr 18, 2019 at 15:14
  • What happens when you run the code above? Commented Apr 18, 2019 at 16:16
  • @brunns This didn't work, same response. Commented Apr 23, 2019 at 8:24
  • @WillKeeling I get an API <Response [404]>, I will contact the Company to see what exactly this response is and get back to you. Commented Apr 23, 2019 at 8:26

2 Answers 2

2

The issue seems to be that you're missing a trailing slash '/' on the end of the URL. Without the trailing slash the server seems to redirect to a non-existent page and you get the 404.

To fix, just add a trailing slash:

requests.post('https://cleardata.proofhub.com/files/upload/', data=data, headers=create_headers)
#                                               Add slash ^
Sign up to request clarification or add additional context in comments.

1 Comment

This got rid of my 404 error, however I am now experiencing another problem where the request doesn't actually do anything. Thank you for your help, you technically solved the question.
1

I think you need to post your file as form data using the files argument.

files = {'file': open(r"C:\Users\dlogan.CLEARDATA\Desktop\ProofHub Upload\test.txt",'rb')
} 
create_headers = {'X-API-KEY': '', 'Content-Type': 'application/json', 'User-Agent': '@cleardata.co.uk'}
r = requests.post('https://cleardata.proofhub.com/files/upload', files=files, headers=create_headers)

You'll need to get the file name right - I can't see the post form, so I don't know what should be.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.