使用 Python 请求发送多部分/表单数据时遇到问题

问题描述 投票:0回答:1

我正在尝试将三个字符串发布到 API/表单。如果您熟悉的话,该 Web 服务称为“Nginx 代理管理器”。它实际上没有 API 文档,但它使用 openapi 3.1.0。我正在尝试发布到此部分: "/nginx/certificates/{certID}/upload": { "post": { "operationId": "uploadCertificate", "summary": "Uploads a custom Certificate", "tags": [ "Certificates" ], "security": [ { "BearerAuth": [ "certificates" ] } ], "parameters": [ { "in": "path", "name": "certID", "schema": { "type": "integer", "minimum": 1 }, "required": true, "example": 1 } ], "requestBody": { "description": "Certificate Files", "required": true, "content": { "multipart/form-data": { "schema": { "type": "object", "additionalProperties": false, "required": [ "certificate", "certificate_key" ], "properties": { "certificate": { "type": "string" }, "certificate_key": { "type": "string" }, "intermediate_certificate": { "type": "string" } } } } } }, "responses": { "200": { "description": "200 response", "content": { "application/json": { "examples": { "default": { "value": { "certificate": "-----BEGIN CERTIFICATE-----\nMIIEYDCCAsigAwIBAgIRAPoSC0hvitb26ODMlsH6YbowDQYJKoZIhvcNAQELBQAw\ngZExHjAcBgNVBAoTFW1rY2VydCBkZXZlbG9wbWVudCBDQTEzMDEGA1UECwwqamN1\ncm5vd0BKYW1pZXMtTGFwdG9wLmxvY2FsIChKYW1pZSBDdXJub3cpMTowOAYDVQQD\nDDFta2NlcnQgamN1cm5vd0BKYW1pZXMtTGFwdG9wLmxvY2FsIChKYW1pZSBDdXJu\nb3cpMB4XDTI0MTAwOTA3MjIxN1oXDTI3MDEwOTA3MjIxN1owXjEnMCUGA1UEChMe\nbWtjZXJ0IGRldmVsb3BtZW50IGNlcnRpZmljYXRlMTMwMQYDVQQLDCpqY3Vybm93\nQEphbWllcy1MYXB0b3AubG9jYWwgKEphbWllIEN1cm5vdykwggEiMA0GCSqGSIb3\nDQEBAQUAA4IBDwAwggEKAoIBAQC1n9j9C5Bes1ndqACDckERauxXVNKCnUlUM1bu\nGBx1xc+j2e2Ar23wUJJuWBY18VfT8yqfqVDktO2wrbmvZvL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OdCQFDjxg/lR7NQOZ1PaZe61aBz5P3pxNqa7ClvMaOsuEQ7w9\nvMYcdtRq8TsjA2JImbSI0TIg8gb2FQxPcYwTJKl+FICOeIwtaSg5hTtJZpnxX5LO\nutTaC0DZjNkTk5RdOdWA8tihyUdGqKoxJY2TVmwGe2rUEDjFB++J4inkEwKBgB6V\ng0nmtkxanFrzOzFlMXwgEEHF+Xaqb9QFNa/xs6XeNnREAapO7JV75Cr6H2hFMFe1\nmJjyqCgYUoCWX3iaHtLJRnEkBtNY4kzyQB6m46LtsnnnXO/dwKA2oDyoPfFNRoDq\nYatEd3JIXNU9s2T/+x7WdOBjKhh72dTkbPFmTPDdAoGAU6rlPBevqOFdObYxdPq8\nEQWu44xqky3Mf5sBpOwtu6rqCYuziLiN7K4sjN5GD5mb1cEU+oS92ZiNcUQ7MFXk\n8yTYZ7U0VcXyAcpYreWwE8thmb0BohJBr+Mp3wLTx32x0HKdO6vpUa0d35LUTUmM\nRrKmPK/msHKK/sVHiL+NFqo=\n-----END PRIVATE KEY-----\n" } } }, "schema": { "type": "object", "additionalProperties": false, "required": [ "certificate", "certificate_key" ], "properties": { "certificate": { "type": "string", "minLength": 1 }, "certificate_key": { "type": "string", "minLength": 1 }, "intermediate_certificate": { "type": "string", "minLength": 1 } } } } } } } } },

这就是我目前拥有的。我尝试了很多其他方法,这似乎是节省空间最正确的方法:

url = "https://api.example.com/api/nginx/certificates/1/upload" # This header has been working in other calls. headers = { "Authorization": f"Bearer " + api_key } # public, private, and chain are all strings # I've tried tuples with None and actual text like "chain.pem" # I've tried a straight dict without the tuple files = { "certificate": (None, public), "certificate_key": (None, private), "intermediate_certificate": (None, chain) } response = requests.post(url, headers=headers, files=files) print(print(json.dumps(response.json(), indent=2))) { "error": "No files were uploaded" }

	
python-3.x python-requests multipartform-data
1个回答
0
投票

© www.soinside.com 2019 - 2024. All rights reserved.