使用 Fedex API 和 Power Query 发布 XML 跟踪请求 [英] Using Fedex API with Power Query to post an XML Track Request

查看:27
本文介绍了使用 Fedex API 和 Power Query 发布 XML 跟踪请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've spent the larger portion of a week trying to figure this thing out and still haven't done it. I'm trying to use the Track API of Fedex within MS Excel Power Query using the XML method. I've gone through all the process of getting TEST credentials and Productions credentials as well.

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

Thanks to @DiegoColantoni amazing feedback to other users I've managed to come up with the following code:

     <?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 中尝试时,它不起作用.我在测试和生产环境中都收到此消息

I have tried this code with Postman and have had successful responses, however when I try it within Power Query it doesn't work. I get this message in both the test and production environments

>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 合作过,我认为这与请求本身有关,但我真的不明白出了什么问题.

Since it has worked with Postman I think it's something to do with the request itself but I don't really understand what is wrong.

这是完整的 Excel Power Query

Here's the full 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

推荐答案

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

FedEx XML Plain Web Services are quite specific as far as the body of the request is concerned: empty lines at the beginning of the xml might result in a 500 response.

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

This is what's happening with your Excel Power Query, see the new lines before and after the actual xml. Removing them should do the trick. I.e. this should work:

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

这篇关于使用 Fedex API 和 Power Query 发布 XML 跟踪请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆