【问题标题】:Python: Adding XML Payload to Requests POST Isn't WorkingPython:将 XML 有效负载添加到请求 POST 不起作用
【发布时间】:2017-02-22 05:50:11
【问题描述】:

我正在向媒体转码 API 提交媒体处理作业。

它基于 REST,并在调用中接受一些值:

  • workflowIdentifier(必需):API 的唯一 ID,这是静态的
  • sourceFilename(必需):url 编码的 samba UNC 路径
  • jobName(可选):字符串
  • context(可选):这是 API 读取的 XML 负载

这是手动提交的示例(为了便于阅读,大部分 XML 被截断)

POST http://1.1.1.1:1234/SubmitRest/SubmitFile?workflowIdentifier=4r5t6y7u&sourceFilename=%5C%5C10.10.1.20%5Cteststore%5Cmedia%5CTEST001E1.mov&jobName=mytestjob&context=%3CContext+xmlns%3D%22http%3A%2F%2FTelestream.Vantage.Sdk%2F2010%2F07%22+xmlns%3Ai%3D%22http%3A%2F%2Fwww.w3.org%2F2001%2FXMLSchem...

当 XML(用于上下文)足够小时,这可行

但是在我们的生产环境中,要发送到“上下文”是一个非常大的 XML 有效负载,所以我得到:

<hr><p>HTTP Error 414. The request URL is too long.</p>

- 所以在做了一些搜索之后,我尝试了以下方法

context_string = (the very large XML document as a string)
source_file =
    '%5C%5C10.10.1.20%5Cteststore%5Cmedia%5CTEST001E1.mov'
job_name = 'testjob'
workflow_id = '4r5t6y7u'
url = 'http://1.1.1.1:1234/SubmitRest/SubmitFile?workflowIdentifier=' + workflow_id + '&sourceFilename=' + source_file + '&jobName=' + job_name
reply = requests.post(url, data={'context': context_string})

作业已成功被 API 接受,但上下文负载被忽略。

(请注意,我已尝试将 XML url 编码为普通字符串)

我也试过这样:

context_string = (the very large XML document as a string)
source_file =
        '%5C%5C10.10.1.20%5Cteststore%5Cmedia%5CTEST001E1.mov'
job_name = 'testjob'
workflow_id = '4r5t6y7u'
payload = {'workflowIdentifier': workflow_id, 'sourceFilename': source_file, 'context': context_stringa, 'jobName': jobname}
reply = requests.post(url, data=payload)

这根本不起作用,它失败了,因为找不到所需的字段(例如workflowIdentifier

完全不明白为什么当我可以将所有数据放入一个 URI 时它可以工作,但是当我尝试使用 requests(data=) 将它添加到有效负载时它不起作用

所以我的主要问题是——我如何通过 python 请求提交它,就像它是 URI POST 调用的一部分一样?我一定在这里做错了什么。

编辑:

所以我在 POST 调用中进行了数据包捕获: 这是我发送的命令:

requests.post(url, data={'context': context_string})

这是抓包的样子:

POST /SubmitRest/SubmitFile?workflowIdentifier=4r5t6y7u&sourceFilename=%5C%5C10.10.1.20%5Cteststore%5Cmedia%5CTEST001E1.mov&jobName=testjob HTTP/1.1
Host: x.x.x.x:1234
Connection: keep-alive
Accept-Encoding: gzip, deflate
Accept: */*
User-Agent: python-requests/2.11.1
Content-Length: 20749

context=<Context xmlns="http://Telestream.Vantage.Sdk/2010/07" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><xmlns i:nil="true" xmlns="http://schemas.datacontract.org/2004/07/Telestream.Soa.Vocabulary" xmlns:a="http://schemas.datacontract.org/2004/07/System.Xml.Serialization"/>
(omitted the rest of the XML)

【问题讨论】:

    标签: python xml rest api python-requests


    【解决方案1】:

    上述问题与Python无关。旧的 Vantage API 需要将 context.xml 作为 URL 的一部分。如果您的 URL 变得太长,则会失败。您可以通过 Postman 进行测试来确认这一点(我的意思是没有 Python。)

    我建议更新到最新的 Vantage (8.0) 及其 API 版本 (5+)。较新的 API 接受 JSON 有效负载。所有变量及其值都作为 JSON 的一部分而不影响 URL。

    新 API 的文档作为 Vantage SDK 的一部分提供。发布URL,您必须创建一个帐户才能访问它。

    我已经尝试过使用 Postman 进行相同的操作,并且可以正常工作。你也可以试试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-29
      • 2018-04-23
      相关资源
      最近更新 更多