You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过API上传图片?解决TypeError: must be str, not InMemoryUploadedFile错误

Alright, let's tackle this TypeError you're hitting when uploading images with requests! The issue is straightforward—you're passing an InMemoryUploadedFile object directly where the API (or your code) expects a file path string. But don't worry, handling in-memory files for upload with requests is totally doable, and here's exactly how to do it:

Step-by-Step Solution

First, remember that file uploads via APIs typically use the multipart/form-data content type, which requests handles seamlessly with the files parameter. Instead of passing a file path, you'll pass the InMemoryUploadedFile object wrapped in a tuple that includes key details the server needs.

Example Code (Common for Django Uploads)

If your InMemoryUploadedFile comes from a Django form (like request.FILES['image']), here's the working upload code:

import requests

# Assume this is your in-memory file (e.g., from Django's request.FILES)
uploaded_file = request.FILES['image']

# Build the files dictionary for the request
# Format: { 'field_name': (filename, file_object, content_type) }
files = {
    'image': (uploaded_file.name, uploaded_file, uploaded_file.content_type)
}

# Send the POST request to your hosting server's API endpoint
api_url = "https://your-hosting-server-api.com/upload"
response = requests.post(api_url, files=files)

# Verify the upload result
if response.ok:
    print("Image uploaded successfully!")
else:
    print(f"Upload failed: {response.status_code} - {response.text}")

Key Details to Note

  • Field Name: Replace 'image' with whatever field name your API expects (check the API docs—common names are 'file', 'upload', etc.).
  • Tuple Structure: The tuple (uploaded_file.name, uploaded_file, uploaded_file.content_type) tells requests exactly how to package the file:
    1. The original filename (so the server knows what to call it)
    2. The in-memory file object itself (since it's a file-like object that supports read(), requests can process it)
    3. The content type (optional but recommended—helps the server recognize the file type)

Adding Extra Data to the Request

If your API requires additional parameters (like a description or category), use the data parameter alongside files:

additional_data = {
    'description': "Vacation photo",
    'album_id': 123
}

response = requests.post(api_url, files=files, data=additional_data)

Why Your Original Approach Failed

When you tried passing the InMemoryUploadedFile directly as a string, Python threw the error because it's not a path—it's an object representing a file stored in memory. By wrapping it in the tuple for the files parameter, you're telling requests to handle it as a file upload instead of a plain string value.

内容的提问来源于stack exchange,提问作者S. Dior

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:35:46