Skip to content

Python request上传图片问题 #611

Answered by chengcuichao
musnows asked this question in 问答
Discussion options

You must be logged in to vote

这样可以解决了,我先下载图片,保存在内存中,之后调用接口上传图片

import requests
from io import BytesIO, BufferedReader

download_url = '123.jpg'

response = requests.get(download_url)

if response.status_code == 200:
    # 创建 BytesIO 对象并将文件内容写入其中
    file_in_memory_bytes = BytesIO(response.content)
    file_in_memory_bytes.name = '123.jpg'
    file_in_memory = BufferedReader(file_in_memory_bytes)

    result = requests.post(
        url=f'{url}/upload',
        headers={
            'Authorization': 'Bearer ',
            'Accept': 'application/json'
        },
        files={'file': file_in_memory}
    )

    print(result.text)
    print(result.json())

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by musnows
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
问答
Labels
None yet
2 participants