Rundeck API 使用 python 上传作业 pycurl 上传文件失败

Rundeck API to upload job using python pycurl fails to upload file

我正在尝试在 python 中使用 pycurl 实现 rundeck 作业导入功能,但找不到该文件。相同的卷曲版本工作正常。

    def importRundeckJOB(jobConfigFilePath):
        responseObject = BytesIO()
        rd = pycurl.Curl()

        rd.setopt(rd.URL, RUNDECK_API_END_POINT)
        rd.setopt(rd.HTTPHEADER, ['X-Rundeck-Auth-Token: '+RUNDECK_TOKEN,
                                  "Accept: application/json"])
        rd.setopt(rd.HTTPPOST, [("dupeOption", "update"),
                                ("fileformat", "yaml"),
                                ("xmlBatch",  jobConfigFilePath )])
        rd.setopt(rd.WRITEFUNCTION, responseObject.write)
        rd.perform()
        responseCode = rd.getinfo(rd.HTTP_CODE)
        rd.close()

错误::

{'error': True, 'apiversion': 17, 'errorCode': 'api.error.jobs.import.missing-file', 'message': 'No file was uploaded'}

工作卷曲

curl --header X-Rundeck-Auth-Token:<TOKEN> -F xmlBatch=@"<FILE_PATH>"  -F dupeOption=update -F fileformat=yaml <RUNDECK_API_END_POINT>/import

{
  "succeeded": [
    {
      "index": 1,
      "id": "deploy-job",
      "name": "deploy-job",
      "group": "DEV",
      "project": "SOF",
      "permalink": "<RUNDECK_URL>/job/show/deploy-job"
    }
  ],
  "failed": [],
  "skipped": []
}

我找到了解决方案,我需要一个额外的参数来处理文件。

http://pycurl.io/docs/latest/quickstart.html#file-upload-multipart-post

c.FORM_FILE

工作解决方案

    def importRundeckJOB(jobConfigFilePath):
        responseObject = BytesIO()
        rd = pycurl.Curl()

        rd.setopt(rd.URL, RUNDECK_API_END_POINT)
        rd.setopt(rd.HTTPHEADER, ['X-Rundeck-Auth-Token: '+RUNDECK_TOKEN,
                                  "Accept: application/json"])
        rd.setopt(rd.HTTPPOST, [("dupeOption", "update"),
                                ("fileformat", "yaml"),
                                ("xmlBatch",  (rd.FORM_FILE, jobConfigFile))])
        rd.setopt(rd.WRITEFUNCTION, responseObject.write)
        rd.perform()
        responseCode = rd.getinfo(rd.HTTP_CODE)
        rd.close()