How to send a “multipart/form-data” with requests in python?

前端 未结 9 1918
野趣味
野趣味 2020-11-22 01:29

How to send a multipart/form-data with requests in python? How to send a file, I understand, but how to send the form data by this method can not understand.

9条回答
  •  没有蜡笔的小新
    2020-11-22 01:39

    Send multipart/form-data key and value

    curl command:

    curl -X PUT http://127.0.0.1:8080/api/xxx ...
    -H 'content-type: multipart/form-data; boundary=----xxx' \
    -F taskStatus=1
    

    python requests - More complicated POST requests:

        updateTaskUrl = "http://127.0.0.1:8080/api/xxx"
        updateInfoDict = {
            "taskStatus": 1,
        }
        resp = requests.put(updateTaskUrl, data=updateInfoDict)
    

    Send multipart/form-data file

    curl command:

    curl -X POST http://127.0.0.1:8080/api/xxx ...
    -H 'content-type: multipart/form-data; boundary=----xxx' \
    -F file=@/Users/xxx.txt
    

    python requests - POST a Multipart-Encoded File:

        filePath = "/Users/xxx.txt"
        fileFp = open(filePath, 'rb')
        fileInfoDict = {
            "file": fileFp,
        }
        resp = requests.post(uploadResultUrl, files=fileInfoDict)
    

    that's all.

提交回复
热议问题