[英]How to pass multipart/form-data into Python POST Request
我正在嘗試將此 curl 命令轉換為 python POST 請求。 我不熟悉調用 api 端點,這是我第一次遇到 -F 表單數據。 我試圖復制的 curl 請求如下:
curl -X POST "https://genericurl.com/rest/endpoint" -H "accept: application/json" -H "Content-Type: multipart/form-data" -F "strictQuotes=" -F "escape=\"" -F "continueOnError=" -F "separator=;" -F "deleteFile=" -F "simulation=" -F "fileName=synchronization_file" -F "headerRow=" -F "ignoreLeadingWhitespace=" -F "sendNotification=" -F "fileId=" -F "template=" -F "saveResult=" -F "batchSize=1000" -F "file=@testSourceFile.csv;type=text/csv" -F "quote=\""
先感謝您!
有一個很好的工具可以幫助您輕松地將 CURL 轉換為 Python 代碼。 你可以看看:
此外,您附加的 CURL 將轉換為類似的內容。
import requests
headers = {
'accept': 'application/json',
# requests won't add a boundary if this header is set when you pass files=
# 'Content-Type': 'multipart/form-data',
}
files = {
'strictQuotes': (None, ''),
'escape': (None, '"'),
'continueOnError': (None, ''),
'separator': (None, ';'),
'deleteFile': (None, ''),
'simulation': (None, ''),
'fileName': (None, 'synchronization_file'),
'headerRow': (None, ''),
'ignoreLeadingWhitespace': (None, ''),
'sendNotification': (None, ''),
'fileId': (None, ''),
'template': (None, ''),
'saveResult': (None, ''),
'batchSize': (None, '1000'),
'file': open('testSourceFile.csv;type=text/csv', 'rb'),
'quote': (None, '"'),
}
response = requests.post('https://genericurl.com/rest/endpoint', headers=headers, files=files)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.