我正在嘗試將此 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 "[email protected];type=text/csv" -F "quote=\""
先感謝您!
uj5u.com熱心網友回復:
有一個很好的工具可以幫助您輕松地將 CURL 轉換為 Python 代碼。你可以看看:
https://curlconverter.com/
此外,您附加的 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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/512923.html
