1

在我的浏览器中加载此链接后,该链接由 USPS 提供的用户 ID 和有关包裹的详细信息组成

Link: http://production.shippingapis.com/ShippingAPITest.dll?API=RateV4&XML=<RateV4Request USERID="[userid]"><Revision/><Package ID="1ST"><Service>PRIORITY</Service><ZipOrigination>02211</ZipOrigination><ZipDestination>90210</ZipDestination><Pounds>5</Pounds><Ounces>2</Ounces><Container>RECTANGULAR</Container><Size>LARGE</Size><Width>15</Width><Length>30</Length><Height>15</Height><Girth>55</Girth></Package></RateV4Request>

我返回了正确的结果(下)

<RateV4Response>
  <Package ID="1ST">
    <ZipOrigination>02211</ZipOrigination>
    <ZipDestination>90210</ZipDestination>
    <Pounds>5</Pounds>
    <Ounces>2</Ounces>
    <Container>RECTANGULAR</Container>
    <Size>LARGE</Size>
    <Width>15</Width>
    <Length>30</Length>
    <Height>15</Height>
    <Zone>8</Zone>
    <Postage CLASSID="1">
      <MailService>Priority Mail 2-Day&lt;sup&gt;&#8482;&lt;/sup&gt;</MailService>
      <Rate>86.65</Rate>
    </Postage>
  </Package>
</RateV4Response>

但是,当尝试使用以下代码块从 Google Apps 脚本编辑器中的函数加载 API 时:

function xmlLoader(){
  var pounds = 5;
  var ounces = 2;
  var userid = "[userid]";
  var url = "http://production.shippingapis.com/ShippingAPI.dll";
  var options =
    {
      "API" : "RateV4",
      "XML" : "<RateV4Request USERID=\"" + userid + "\"> \
                 <Revision/> \
                 <Package ID=\"1ST\"> \
                   <Service>PRIORITY</Service> \
                   <ZipOrigination>02211</ZipOrigination> \
                   <ZipDestination>90210</ZipDestination> \
                   <Pounds>" + pounds + "</Pounds> \
                   <Ounces>" + ounces + "</Ounces> \
                   <Container>RECTANGULAR</Container> \
                   <Size>LARGE</Size> \
                   <Width>15</Width> \
                   <Length>30</Length> \
                   <Height>15</Height> \
                   <Girth>55</Girth> \
                 </Package> \
               </RateV4Request>"
    };

  var response = UrlFetchApp.fetch(url, options);
  Logger.log(response.getContentText());
};

这是我返回的错误,

<?xml version="1.0" encoding="UTF-8"?>
  <Error>
    <Number>80040B19</Number>
    <Description>XML Syntax Error: Please check the XML request to see if it can be parsed.</Description>
    <Source>USPSCOM::DoAuth</Source>
  </Error>

任何帮助将不胜感激

4

2 回答 2

2

我没有任何 usps ID,因此无法进行完整测试,但对我而言,问题来自您提供的 URLFetch 参数。试试这个代码:

function xmlLoader(){
  var pounds = 5;
  var ounces = 2;
  var userid = "1000"; //"[userid]";
  var url = "http://production.shippingapis.com/ShippingAPI.dll";
  var payload =
    {
      "API" : "RateV4",
      "XML" : "<RateV4Request USERID=\"" + userid + "\"> \
                 <Revision/> \
                 <Package ID=\"1ST\"> \
                   <Service>PRIORITY</Service> \
                   <ZipOrigination>02211</ZipOrigination> \
                   <ZipDestination>90210</ZipDestination> \
                   <Pounds>" + pounds + "</Pounds> \
                   <Ounces>" + ounces + "</Ounces> \
                   <Container>RECTANGULAR</Container> \
                   <Size>LARGE</Size> \
                   <Width>15</Width> \
                   <Length>30</Length> \
                   <Height>15</Height> \
                   <Girth>55</Girth> \
                 </Package> \
               </RateV4Request>"
    };

  var options={
    method:"POST",
    payload:payload
  }

  var response = UrlFetchApp.fetch(url, options);
  Logger.log(response.getContentText());
};

看看这里的文档

于 2014-03-07T08:33:25.260 回答
1

还要记住,USPS 和 UPS 都没有最好的 XML 解释器。我发现这篇文章正在搜索为公司名称中的 & 符号解析 HTML 实体的语法问题。(这应该在 XML 中得到支持,但显然它会导致 USPS 呕吐)其中一些对“订单”也非常挑剔。如在,如果不完全按照他们的文档指定的元素顺序将导致错误。我还遇到了他们“声称”是可选的字段的问题,但事实并非如此——我需要发送一个“空”标签——以及其他在空标签上呕吐的问题。我昨天遇到的另一件事,验证地址 api 似乎喜欢翻转地址 1 和地址 2,但仅限于输出。(如果您只发送地址2,输入时它会呕吐)。

于 2014-05-28T15:14:50.203 回答