Help Needed: Uploading tarfile using python requests library's post

I am trying to upload tgz files to a server using the python requests library. I am not sure how to pass the files to be uploaded. The curl command that I have to convert into a python request is as below:

```shell
curl -X POST 'Content-Type: multipart/form-data' -H "x-auth-token: $token" -H 'Accept: application/json' https://<some_server>/api/url -F "file=@$1"    # $1 is the name of the file
```

### First Attempt
I pass a file handle in a dictionary using the `open()` function as below:
```python
import requests

data = {'file':open('tgz_file.tgz')}
headers = {'x-auth-token': 'some_token', 'Accept': 'application/json', 'Content-Type': 'multipart/form-data'}
requests.post(url=https://<some_server>/api/url, headers=headers, data=data)
```

The error I get is as below:
```shell
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.6/site-packages/requests/api.py", line 112, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/usr/lib/python3.6/site-packages/requests/api.py", line 58, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/lib/python3.6/site-packages/requests/sessions.py", line 516, in request
    prep = self.prepare_request(req)
  File "/usr/lib/python3.6/site-packages/requests/sessions.py", line 448, in prepare_request
    hooks=merge_hooks(request.hooks, self.hooks),
  File "/usr/lib/python3.6/site-packages/requests/models.py", line 305, in prepare
    self.prepare_body(data, files, json)
  File "/usr/lib/python3.6/site-packages/requests/models.py", line 502, in prepare_body
    body = self._encode_params(data)
  File "/usr/lib/python3.6/site-packages/requests/models.py", line 99, in _encode_params
    for v in vs:
  File "/usr/lib64/python3.6/codecs.py", line 321, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb3 in position 0: invalid start byte
```

I assumed this to be an issue with `open()` not being able to read the tar file correctly. So I tried the second option.

### Second Attempt - Use the tarfile module

```python
import requests
import tarfile

data = {'file':tarfile.open('tgz_file.tgz', mode='r:gz')}
headers = {'x-auth-token': 'some_token', 'Accept': 'application/json', 'Content-Type': 'multipart/form-data'}
requests.post(url=https://<some_server>/api/url, headers=headers, data=data)
```

However, this lead to a different issue:
```shell
'{"statusCode":400,"error":"Bad Request","message":"Invalid content-type header: multipart missing boundary"}'
```

### Third Attempt - Removed *Content-Type* from the headers

```python
import requests
import tarfile

data = {'file':tarfile.open('tgz_file.tgz', mode='r:gz')}
headers = {'x-auth-token': 'some_token', 'Accept': 'application/json'}
requests.post(url=https://<some_server>/api/url, headers=headers, data=data)
```

This results in:
```shell
{'statusCode': 415, 'error': 'Unsupported Media Type', 'message': 'Unsupported Media Type'}
```

I am not sure how to go about solving this issue. Any help will be be greatly appreciated.

Hi @moijes12,

This is all covered in the [documentation](https://requests.readthedocs.io/en/master/user/quickstart/#post-a-multipart-encoded-file). If you have further questions, please consult [StackOverflow](https://stackoverflow.com) since we do not triage usage tickets on this tracker. Thanks!

@nateprewitt I could not find a satisfactory answer on StackOverflow. I have asked a question there. I was hoping someone on this tracker would know. 

@moijes12 this is a defect tracker not a "I haven't received an answer on StackOverflow quickly enough" tracker. And the answer is very much in the documentation. Please spend time with it