curl 如何将多部分/表单数据传递到Python POST请求

1rhkuytd  于 2022-11-13  发布在  Python
关注(0)|答案(1)|浏览(190)

我正在尝试将这个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=\""

先谢谢你!

2wnc66cl

2wnc66cl1#

有一个很好的工具可以帮助你轻松地将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)

相关问题