简体   繁体   中英

How to upload csv file to API using python

Tried this

response = requests.put(url, headers=headers, files = files)

where file is

files = [
  ('file', open('D:/cf_upload-sheet/new_data.csv','rb'))
]

Getting this error on server end: ,"method":"PUT","exception":"RuntimeError - Only.csv, .xls and.xlsx files are allowed."

Did anyone face the same issue? and how to resolve it?

Can you try to make it a dictionary instead?

files = {'file': open('D:/cf_upload-sheet/new_data.csv','rb')}

EDIT: I think it is possible to send other details about the file based on this: https://requests.readthedocs.io/en/master/user/quickstart/#post-a-multipart-encoded-file

files = {
    "file": (
        "new_data.csv",
        open("D:/cf_upload-sheet/new_data.csv", "rb"),
        "text/csv",
        {"Expires": "0"},
    )
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM