[英]python send image url on requests
usertoken = requests.request("GET", "http://127.0.0.1:8000/en/get-user-by-email?email={}".format("soubhagyakumar666@gmail.com"))
usertoken = usertoken.json()["user"]["token"]
url = "http://127.0.0.1:8000/en/update-project-thumbnail"
payload = {
"project_id": 6885
}
files=[
('project_image',('{}'.format("_0005_MainCam_42.png"),
'https://d3lwp4d5yh6xdg.cloudfront.net/static/artwork_project/6886/_0005_MainCam_42.png','rb', 'image/jpeg')),
]
headers = {
'Authorization': 'Token '+ usertoken
}
response = requests.request("PUT", url, headers=headers, data=payload, files=files)
print(response.status_code, ":status_code")
在這里,我通過請求發送圖像。 但是,我得到了錯誤。 我必須通過 url 發送圖像可能嗎?
File "/Users/soubhagyapradhan/Desktop/upwork/africa/backend/env/lib/python3.8/site-packages/requests/models.py", line 317, in prepare
self.prepare_body(data, files, json)
File "/Users/soubhagyapradhan/Desktop/upwork/africa/backend/env/lib/python3.8/site-packages/requests/models.py", line 505, in prepare_body
(body, content_type) = self._encode_files(files, data)
File "/Users/soubhagyapradhan/Desktop/upwork/africa/backend/env/lib/python3.8/site-packages/requests/models.py", line 165, in _encode_files
rf = RequestField(name=k, data=fdata, filename=fn, headers=fh)
File "/Users/soubhagyapradhan/Desktop/upwork/africa/backend/env/lib/python3.8/site-packages/urllib3/fields.py", line 155, in __init__
self.headers = dict(headers)
ValueError: dictionary update sequence element #0 has length 1; 2 is required
ERROR 2021-05-19 11:47:06,121 basehttp 16077 123145380831232 "GET /en/signup H
問題出在下面的代碼上:
response = requests.request("PUT", url, headers=headers, data=payload, files=files)
文件(變量值)需要是字典數據結構,如圖所示:
correct_file={'file':("project_image ( {0})".format("_0005_MainCam_42.png"),
open('out.png'
, 'rb'),'image/png')} # replace out.png with your file path
所以請求將如下所示:
response = requests.request("PUT", url, headers=headers, data=payload, files=correct_file)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.