使用 Fedex API 和 Power Query 发布 XML 跟踪请求

时间:2021-04-22 01:12:30

标签: xml api xmlhttprequest powerquery fedex

我花了一周的大部分时间试图弄清楚这件事,但仍然没有完成。我正在尝试使用 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

1 个答案:

答案 0 :(得分: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