【问题标题】:Using Fedex API with Power Query to post an XML Track Request使用 Fedex API 和 Power Query 发布 XML 跟踪请求
【发布时间】:2021-07-16 05:09:37
【问题描述】:

我花了一周的大部分时间试图弄清楚这件事,但仍然没有完成。我正在尝试使用 XML 方法在 MS Excel Power Query 中使用 Fedex 的 Track API。我也完成了获取 TEST 凭证和 Productions 凭证的所有过程。

感谢@DiegoColantoni 对其他用户的惊人反馈,我设法想出了以下代码:

     <?xml version="1.0" encoding="UTF-8"?>
     <TrackRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://fedex.com/ws/track/v19">
           <WebAuthenticationDetail>
                <UserCredential>
                    <Key>MYKEY</Key>
                    <Password>MYPWD</Password>
                </UserCredential>
            </WebAuthenticationDetail>
            <ClientDetail>
                <AccountNumber>MYACCOUNT</AccountNumber>
                <MeterNumber>MYMETER</MeterNumber>
            </ClientDetail>
            <TransactionDetail>
                <CustomerTransactionId>TestTest</CustomerTransactionId>
            </TransactionDetail>
            <Version>
                <ServiceId>trck</ServiceId>
                <Major>19</Major>
                <Intermediate>0</Intermediate>
                <Minor>0</Minor>
            </Version>
            <SelectionDetails>
                <PackageIdentifier>
                    <Type>TRACKING_NUMBER_OR_DOORTAG</Type>
                    <Value>785459309647</Value>
                </PackageIdentifier>
            </SelectionDetails>
        </TrackRequest>

我已经用 Postman 尝试过这段代码,并且得到了成功的响应,但是当我在 Power Query 中尝试它时它不起作用。我在测试和生产环境中都收到此消息

>DataSource.Error: Web.Contents failed to get contents from 'https://ws.fedex.com/xml' (500): Internal Server Error
Details:
    DataSourceKind=Web
    DataSourcePath=https://ws.fedex.com/xml
    Url=https://ws.fedex.com/xml
 code 

由于它已与 Postman 合作,因此我认为这与请求本身有关,但我真的不明白出了什么问题。

这是完整的 Excel Power Query

let
   url = "https://ws.fedex.com:443/xml",
   Body = Text.ToBinary("
        <?xml version=""1.0"" encoding=""UTF-8""?>
        <TrackRequest xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns=""http://fedex.com/ws/track/v19"">
            <WebAuthenticationDetail>
                <UserCredential>
                    <Key>MYKEY</Key>
                    <Password>MYPWD</Password>
                </UserCredential>
            </WebAuthenticationDetail>
            <ClientDetail>
                <AccountNumber>MYACCT</AccountNumber>
                <MeterNumber>MYMETER</MeterNumber>
            </ClientDetail>
            <TransactionDetail>
                <CustomerTransactionId>PruebaPrueba</CustomerTransactionId>
            </TransactionDetail>
            <Version>
                <ServiceId>trck</ServiceId>
                <Major>19</Major>
                <Intermediate>0</Intermediate>
                <Minor>0</Minor>
            </Version>
            <SelectionDetails>
                <PackageIdentifier>
                    <Type>TRACKING_NUMBER_OR_DOORTAG</Type>
                    <Value>785459309647</Value>
                </PackageIdentifier>
            </SelectionDetails>
        </TrackRequest>
    "),
    Source = Web.Contents(url, [Headers=[Accept="image/gif, image/jpeg, image/pjpeg, text/plain, text/html, */*", #"Content-Type"="text/xml"], Content = Body])
in
    Source

【问题讨论】:

  • 我感到受宠若惊!谢谢。虽然我现在也感受到了压力。不幸的是,我没有 Power Query 来尝试你的代码,我只能用眼睛观察它。据我所知,正文开头和结尾的 new lines 可能是您的查询失败的原因。试试:Body = Text.ToBinary(""),。抱歉,我不确定这是否会对您有所帮助,因此我将其发布在评论中。如果我不清楚,请告诉我,我会用更好的格式写一个正确的答案。
  • 好吧,@Diego 先生,您是天使。 Fedex 应该付钱给你……我按照你的建议做了,就是将整个正文与Text.ToBinary 内联,它起作用了……所以,是的,Excel 函数也可以将文本转换为二进制编码的换行符,在邮递员换行符被忽略,所以这就是它在那里工作的原因。如果您关心这些事情,您可以发表您的评论作为答案,以便我将其标记为解决方案。如果没有,那么非常感谢。

标签: xml api xmlhttprequest powerquery fedex


【解决方案1】:

就请求正文而言,FedEx XML 纯 Web 服务非常具体:xml 开头的空行可能会导致 500 响应。

这就是您的 Excel Power Query 发生的情况,请查看实际 xml 前后的新行。删除它们应该可以解决问题。 IE。这应该工作:

let
   url = "https://ws.fedex.com:443/xml",
   Body = Text.ToBinary("<?xml version=""1.0"" encoding=""UTF-8""?>
        <TrackRequest xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns=""http://fedex.com/ws/track/v19"">
            <WebAuthenticationDetail>
                <UserCredential>
                    <Key>MYKEY</Key>
                    <Password>MYPWD</Password>
                </UserCredential>
            </WebAuthenticationDetail>
            <ClientDetail>
                <AccountNumber>MYACCT</AccountNumber>
                <MeterNumber>MYMETER</MeterNumber>
            </ClientDetail>
            <TransactionDetail>
                <CustomerTransactionId>PruebaPrueba</CustomerTransactionId>
            </TransactionDetail>
            <Version>
                <ServiceId>trck</ServiceId>
                <Major>19</Major>
                <Intermediate>0</Intermediate>
                <Minor>0</Minor>
            </Version>
            <SelectionDetails>
                <PackageIdentifier>
                    <Type>TRACKING_NUMBER_OR_DOORTAG</Type>
                    <Value>785459309647</Value>
                </PackageIdentifier>
            </SelectionDetails>
        </TrackRequest>"),
    Source = Web.Contents(url, [Headers=[Accept="image/gif, image/jpeg, image/pjpeg, text/plain, text/html, */*", #"Content-Type"="text/xml"], Content = Body])
in
    Source

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-22
    • 2010-12-29
    • 1970-01-01
    相关资源
    最近更新 更多