| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- KAKAO
- pytorch
- judge
- 책갈피
- DB
- PostgreSQL
- Paper
- pandas
- evaluation
- TORCH
- Converting
- file
- git
- Database
- Python
- list
- GitLab
- AI
- Container
- Mac
- LLM
- Package
- ubuntu
- Linux
- Laravel
- numpy
- format
- docker
- Flask
- Windows
Archives
- Today
- Total
Daily Develope
[Web] Multipart/form-data 요청(Request) in Python 본문
"requests" 모듈을 사용하는 경우 Content-Type을 명시 혹은 생략하는 2가지 방법이있다.
만약 호출하려는 api 에서 Content-Type(~Header)을 검사하는 경우라면 전자(Content-Type 명시)의 방법을, 아니라면 후자의 방법을 사용하면된다.
방법1) Content-Type 명시
: Header에서 Content-Type으로 MultipartEncoder로 생성해 boundary가 추가된 content-type을 받아 사용한다. (pre-cond. requests_toolbelt 설치)
import requests
from requests_toolbelt import MultipartEncoder
_data = MultipartEncoder(fields=[("tfile", open("my/file/path.ext", "rb"))])
_headers = {
"Content-Type": _data.content_type,
"Accept": "*/*"
# ...
}
response = requests.post("https://api-url.com", headers=_headers, data=_data)
방법2) Content-Type 생략
: Header에서 Content-Type을 생략하고 files로 데이터를 전송한다. 이 경우 requests 내부적으로 boundary를 추가해 전송한다.
import requests
_files = {
"tfile": open("my/file/path.ext", "rb")
}
_headers = {
"Accept": "*/*"
# ...
}
response = requests.post("https://api-url.com", headers=_headers, files=_files)'Develope > Web' 카테고리의 다른 글
| [Web] XPath 간단 정리 (0) | 2023.05.09 |
|---|---|
| [Web] curl & program code 변환 (관련링크) (0) | 2023.03.27 |
| [Request] Content-Type의 데이터별 http 요청(requests) 코드 예제 (0) | 2022.11.07 |
| [HTTP] 응답 상태코드 (response status code) (0) | 2022.08.17 |
| [FLASK] request 정리 (0) | 2022.08.16 |
Comments