2022-02-28 TIL

Grolar Kim·2022년 2월 28일
0

TIL-WIL

목록 보기
6/17

파이썬에서 form으로 파일을 받아서 requests나 httpx로 전달하려고 할 때 오류가 생겼었는데, data와 files를 분리하고 file.getvalue()를 사용하여 해결했다.

imgs = request.FILES["imgs"].file.getvalue()
today = datetime.now()
mytime = today.strftime('%Y-%m-%d-%H-%M-%S')
data = {'key' : mytime}
files = {'img' : imgs}
r = httpx.post('http://127.0.0.1:8001/api/v1/', data=data, files=files)
url = r.json() # success

출처 : https://stackoverflow.com/questions/51189487/django-post-inmemoryuploadedfile-to-external-rest-api

https://www.python-httpx.org/quickstart/#sending-multipart-file-uploads

0개의 댓글