0

I tried to send file with some headers like:

files = {'file': (file, open(file, 'rb'), {'Content-type': 'multipart/form-data; boundary=---BOUNDARY', 'Authorization' : 'Basic ' + api_key})}

r = requests.post(base_url, files=files)

Server returned 401 error that means absent header Authorization. But I sent it

2 Answers 2

1

For Basic Authentication you can follow the requests docs. It's visible on the very first line of code in the example on that page.

Use the auth keyword argument to supply a 2-tuple of username and password:

response = requests.post(base_url, files=files, auth=('username', 'password'))

Edit:

If you want to send actual headers, rather than things like Basic Auth, you can do that with the headers keyword argument. This allows you to give a dict of headers you would like to send. For example:

headers = {'Content-Type': 'application/json'}\
response = requests.post(url, data=data, headers=headers)

The auth argument, should be a simplification of the above because Basic Auth is so common, but don't quote me on that one.

Sign up to request clarification or add additional context in comments.

3 Comments

I asked about headers, not about Basic Authentication, also your code does not work too
API says to send token in header when I attempt upload file
@Oleg Good to know it works, edited answer for headers anyway.
0

You can follow this example to take it from the official documentation I think you should try it first in postman, with the json then and do it from python.

This information is taken from the official documentation python

1 Comment

This is better to be a comment , because this is not actual answer , this is a test approach.

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.