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

使用Python Requests通过POST请求上传图片至API后读取返回结果的技术咨询

Hey there! Let's work through this problem together—you're already halfway there with the POST upload, so let's fix the response handling part and make the whole flow work smoothly.

First, Tweak the Upload Code (Small but Important)

Your current upload code is almost right, but you should open the image file in binary read mode (rb) to avoid encoding issues across different operating systems. Using a with statement also ensures the file gets properly closed after upload, which is good practice:

with open("img.bmp", "rb") as img_file:
    files = {"img": img_file}
    r = requests.post("your-api-url-here", files=files)

Next: Handling the API's Returned Image

The key here is figuring out what format the API sends back the processed image. There are two common scenarios, and I'll cover both:

Scenario 1: API Returns Raw Binary Image Data

Most image-processing APIs send the processed image directly as binary data (with a Content-Type header like image/bmp). To save this, use r.content (which gives you raw bytes) and write it to a file:

# First, confirm the request succeeded
if r.status_code == 200:
    # Save the processed image to a file
    with open("processed_image.bmp", "wb") as output_file:
        output_file.write(r.content)
    print("Processed image saved successfully!")
else:
    # If something went wrong, print error details
    print(f"Request failed with status code: {r.status_code}")
    print("API error message:", r.text)

Scenario 2: API Returns JSON with a Base64-Encoded Image

Some APIs wrap the image in a JSON response (using base64 to convert binary to text). In this case, parse the JSON, decode the base64 string, then save the bytes:

import base64

if r.status_code == 200:
    # Parse the JSON response
    response_data = r.json()
    # Replace 'processed_img' with the actual key your API uses (check docs!)
    if "processed_img" in response_data:
        # Decode base64 string to binary
        image_bytes = base64.b64decode(response_data["processed_img"])
        # Save the image
        with open("processed_image.bmp", "wb") as output_file:
            output_file.write(image_bytes)
        print("Processed image from JSON saved successfully!")
    else:
        print("JSON response doesn't have the expected 'processed_img' field.")
else:
    print(f"Request failed: {r.status_code}")
    print(r.text)

Why GET Doesn't Work for Image Uploads

Quick side note: You mentioned trying GET for image uploads—don't waste time on that! GET requests send data in the URL, which has strict length limits and isn't designed for binary files like images. POST with the files parameter is the standard, correct approach here.

Troubleshooting Tips if It Still Fails

  • Check API docs: Make sure the parameter name (img in your code) matches what the API expects. Some APIs use file or image instead.
  • Inspect response headers: Run print(r.headers['Content-Type']) to see what format the API returns. This tells you if you need to handle binary or JSON.
  • Catch errors properly: Use r.raise_for_status() to automatically throw an exception for HTTP errors (like 404, 500) to debug issues faster.

Full Working Example

Here's a complete, robust script that handles both scenarios and error cases:

import requests
import base64

def upload_and_process_image(api_url, input_path, output_path):
    try:
        # Upload the image
        with open(input_path, "rb") as img_file:
            files = {"img": img_file}
            r = requests.post(api_url, files=files)
            r.raise_for_status()  # Raise error for HTTP status codes >=400

        # Determine how to handle the response
        content_type = r.headers.get("Content-Type", "")
        if "image/" in content_type:
            # Raw binary image
            with open(output_path, "wb") as out_file:
                out_file.write(r.content)
            print(f"Success! Processed image saved to {output_path}")
        elif "application/json" in content_type:
            # JSON with base64 image
            response_json = r.json()
            if "processed_img" in response_json:
                img_bytes = base64.b64decode(response_json["processed_img"])
                with open(output_path, "wb") as out_file:
                    out_file.write(img_bytes)
                print(f"Success! Processed image from JSON saved to {output_path}")
            else:
                print("Error: JSON response missing 'processed_img' field")
        else:
            print(f"Unsupported response type: {content_type}")
            print("Response content:", r.text)

    except requests.exceptions.RequestException as e:
        print(f"Request failed: {str(e)}")
    except Exception as e:
        print(f"Unexpected error: {str(e)}")

# Usage
upload_and_process_image(
    api_url="your-api-endpoint",
    input_path="img.bmp",
    output_path="processed_img.bmp"
)

内容的提问来源于stack exchange,提问作者glory9211

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 03:58:18