---
title: "How can you upload a file to an API using Python?"  
description: "How can you upload a file to an API using Python?"  
author: "Anubhav Sharma"  
published: 2025-11-06  
updated: 2025-11-21  
canonical: https://www.mindstick.com/forum/161989/how-can-you-upload-a-file-to-an-api-using-python  
category: "python"  
tags: ["python-3.4", "Python 3"]  
reading_time: 2 minutes  

---

# How can you upload a file to an API using Python?

**How can you [upload](https://www.mindstick.com/forum/12860/how-to-upload-file-asynchronously-using-generic-handler) a [file](https://www.mindstick.com/articles/59/encrypting-and-decrypting-files-using-c-sharp) to an API using Python?**

## Replies

### Reply by ICSM Computer

> To **upload a file to an** [**API using Python**](https://www.mindstick.com/articles/340967/designing-a-rest-api-using-python), the most common method is to use the `requests` library with a [**multipart/form-data** request](https://franklingu.github.io/programming/2017/10/30/post-multipart-form-data-using-requests/).

## Basic Example — Upload a File

```python
import requests

url = "https://example.com/api/upload"
file_path = "C:/Users/Anna/Desktop/photo.jpg"

with open(file_path, "rb") as file:
    files = {
        "file": (file_path, file, "image/jpeg")  # (filename, file-data, MIME type)
    }
    response = requests.post(url, files=files)

print(response.status_code)
print(response.text)
```

## Upload With Additional Form Data

If the API needs extra fields (e.g., userId, title):

```python
import requests

url = "https://example.com/api/upload"

data = {
    "userId": "12345",
    "title": "Profile Picture"
}

with open("photo.jpg", "rb") as file:
    files = { "file": ("photo.jpg", file) }
    response = requests.post(url, data=data, files=files)

print(response.json())
```

## Upload Using Bearer Token / Authentication

If the [API requires authentication](https://www.mindstick.com/interview/34408/how-do-you-handle-api-authentication-in-python-bearer-token):

```python
import requests

url = "https://example.com/api/upload"
token = "YOUR_ACCESS_TOKEN"

headers = {
    "Authorization": f"Bearer {token}"
}

with open("video.mp4", "rb") as f:
    files = { "file": ("video.mp4", f) }
    resp = requests.post(url, headers=headers, files=files)

print(resp.status_code, resp.text)
```

## Upload Multiple Files in One Request

```python
import requests

url = "https://example.com/api/upload"

files = [
    ("files", ("image1.jpg", open("image1.jpg", "rb"))),
    ("files", ("image2.jpg", open("image2.jpg", "rb")))
]

response = requests.post(url, files=files)
print(response.json())
```

## Important Notes

| Issue | Fix |
| --- | --- |
| `415 Unsupported Media Type` | Use `files=`, not `data=` for file content |
| `401 Unauthorized` | Add token/header correctly |
| Upload gets corrupted | Ensure binary mode `"rb"` when opening the file |

## If API Requires JSON Instead of Multipart

Some APIs expect [Base64 inside JSON](https://www.mindstick.com/interview/34374/encode-and-decode-string-function-in-python):

```python
import base64
import json
import requests

with open("file.pdf", "rb") as f:
    encoded = base64.b64encode(f.read()).decode()

payload = {
    "fileName": "file.pdf",
    "fileBase64": encoded
}

resp = requests.post("https://example.com/api/upload", json=payload)
print(resp.json())
```


---

Original Source: https://www.mindstick.com/forum/161989/how-can-you-upload-a-file-to-an-api-using-python

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
