我有以下 curl 命令
file_location='some/path/to/test.csv'
auth_token='my_api_token'
curl --insecure --request POST 'https://<ip_address>/eds/api/table/table_name/changes/addBulk?hasHeader=false' -H 'Csrf-Token: nocheck' -H 'AuthToken:'$auth_token'' -H 'Content-Type: multipart/form-data' --form 'data=@'$file_location'
csv 中的資料只是多行 1 列,例如:
row_1
row_2
row_3
curl 命令作業得很好,我正在嘗試為它獲取 python 替代品,我在下面嘗試:
files = {'file': ('test.csv', open("some/path/to/test.csv", 'rb'), 'multipart/form-data')}
auth_token="<api token here>"
url="https://" ip_address "/eds/api/table/" table_name "/changes/addBulk?hasHeader=false"
headers = {
'Csrf-Token': 'nocheck',
'AuthToken': auth_token
}
response = requests.post(url, headers=headers, files=files, verify=False)
任何幫助表示贊賞
uj5u.com熱心網友回復:
我注意到在你的 curl url 中你在 "/api/..." 之前有 "eds" 但在你的 python 版本中你沒有。
curl_url = "https://<ip_address>/eds/api/table/table_name/changes/addBulk?hasHeader=false"
python_url = "https://" ip_address "/api/table/" table_name "/changes/addBulk?hasHeader=false"
此外,在 python 3 中,使用這樣的 f 字串更簡潔:
url = f"https://{ip_address}**/eds/**api/table/{table_name}/changes/addBulk?hasHeader=false"
其他一切對我來說都是正確的。將來,在回應上設定斷點以查看 400 http 回應是否有任何其他資訊可能會有所幫助。
編輯:
好的,您可以嘗試將標題修改為:
headers = {
'Csrf-Token': 'nocheck',
'AuthToken': auth_token,
'Content-Type': 'multipart/form-data'
}
還將您的檔案編輯為:
files = {
'data': ('test.csv', open("some/path/to/test.csv", 'rb'), 'multipart/form-data')
}
最終代碼應為:
auth_token = "<api token here>"
files = {
'data': ('test.csv', open("some/path/to/test.csv", 'rb'), 'multipart/form-data')
}
url = f"https://{ip_address}/eds/api/table/{table_name}/changes/addBulk?hasHeader=false"
headers = {
'Csrf-Token': 'nocheck',
'AuthToken': auth_token
}
response = requests.post(url, headers=headers, files=files, verify=False)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/477267.html
標籤:python-3.x api 卷曲 蟒蛇请求
