编辑:关于乔纳森的评论:
运行此代码时出现以下错误:
无效参数:http://production.shippingapis.com/ShippingAPI.dll?API=Verify&XML= 6406%20Ivy%20LaneGreenbeltMD
以下是相关代码。我没有包含我的用户ID:
function validate_address() {
//var userID = 'xxxxxxxxxxx';
//var address1 = '204 main street apt 4';
//var address2 = '';
//var city = '';
//var state = '';
//var zip5 = '12601';
//var zip4 = '';
//Will switch out hard coded values with variables once I get it working.
var XMLhttp_request = 'http://production.shippingapis.com/ShippingAPI.dll?API=Verify&XML=<AddressValidateRequest USERID="xxxxxxxxxxx"><Address><Address1></Address1><Address2>6406 Ivy Lane</Address2><City>Greenbelt</City><State>MD</State><Zip5></Zip5><Zip4></Zip4></Address></AddressValidateRequest>';
var response = UrlFetchApp.fetch(XMLhttp_request, options);
Logger.log(response.getContentText());
}
USPS地址验证API在其网站上有一些示例代码。当我使用带有我的用户ID的代码时,将其直接复制并粘贴到它工作的URL栏中,我就可以找到我正在寻找的XML。
答案 0 :(得分:1)
所以我自己想出来了。这是帮助我的文档: Google apps scripts documentation Parsing XML
我之前代码中的错误是将URL与api和参数组合在一起。您可以在下面看到它们现在是分开的,查询作为传入的对象作为options参数。有一点需要注意的是&#34;?&#34;查询之前的符号不在我的有效负载变量的开头。虽然将它直接粘贴到地址栏时包含在USPS文档中,但在G.A.S中你将它留下来。
这是适合我的代码。希望这有助于其他人。
function validate_address() {
//var userID = 'xxxxxxxxxxx';
//var address1 = '204 main street apt 4';
//var address2 = '';
//var city = '';
//var state = '';
//var zip5 = '12601';
//var zip4 = '';
//Will switch out hard coded values with variables once I get it working.
var payload = 'API=Verify&XML=<AddressValidateRequest USERID="xxxxxxxxxxxx"><Address><Address1></Address1><Address2>6406 Ivy Lane</Address2><City>Greenbelt</City><State>MD</State><Zip5></Zip5><Zip4></Zip4></Address></AddressValidateRequest>';
var url = 'http://production.shippingapis.com/ShippingAPI.dll';
var options = {
'method': 'get',
'headers': {
"Accept": "application/xml"
},
'payload': payload
};
var response = UrlFetchApp.fetch(url, options);
Logger.log(response.getContentText());
//Logger.log(XMLhttp_request);
}