使用 Swift 访问 Shippo API 返回未记录的错误

Posted

技术标签:

【中文标题】使用 Swift 访问 Shippo API 返回未记录的错误【英文标题】:Using Swift to access Shippo API Returns undocumented error 【发布时间】:2017-12-17 20:14:16 【问题描述】:

我们正在尝试使用 Shippo API 打印发货标签,但似乎无法正常工作。这里代码使用的数据直接来自七宝官网。

我们正在使用 Swift、Alamofire 和 SwiftyJSON 以及来自我们 ios 应用程序的 Swift 代码,并得到这个错误结果:


"detail" : "Shippo API could not process your request. Request ID: 8af7a4c883cd453bb492ad2782bd54d3"

我们已经搜索了 Shippo 网站、*** 和 Google,但找不到与此错误相关的任何参考。

这是 Swift 代码:

func shippoRequest() 

var headers: HTTPHeaders = [
"content-type": "application/json"
]

let credentials = "shippo_test_318550b6ebde57479ef555ae7f80e660c23ee6c6"
headers["Authorization"] = "ShippoToken \(credentials)"
let parameters: [String: Any] = [
"address_from": [
[
"name" : "Shippy Shipper",
"company": "Our Shippo App",
"street1" : "101 Lafayette Road",
"street2" : "",
"city" : "Rye",
"state" : "NH",
"zip" : "03870",
"phone" : "8443872777",
"country" : "US",
"email" : "cuzineed1@yahoo.com"
]
],
"address_to": [
[
"name": "Mrs. Hippo",
"street1": "965 Mission St.",
"city": "San Francisco",
"state": "CA",
"zip": "94103",
"country": "US",
"phone": "0987654321",
"email": "support@goshippo.com"
]
],
"async": false,
"parcels": [
[
"length": "5",
"width": "5",
"height": "5",
"distance_unit": "in",
"weight": "2",
"mass_unit": "lb"
]
]
]
let url = "https://api.goshippo.com/shipments"

Alamofire.request(url, method: .post, parameters: parameters , encoding: JSONEncoding.default, headers: headers)
.responseJSON  (response) in
switch response.result 
case .success(let value):
let swiftyJson = JSON(value)
print ("return as JSON using swiftyJson is: \(swiftyJson)")
case .failure(let error):
print ("error: \(error)")




我们尝试了许多不同的地址,往返,甚至使用相同的数据在shippo网站上使用测试链接,它确实有效。

如果 Shippo 返回此错误,我们希望有人以前看到过,或者 Shippo 应该能够解释是什么无效数据导致了此错误。

任何帮助将不胜感激。

谢谢

【问题讨论】:

【参考方案1】:

我相信地址字段中缺少“object_ purpose”属性。这在 API 的“2017-03-29”版本中已被弃用,因此它可能取决于您是否使用旧版本的 API。

https://goshippo.com/docs/changelog

【讨论】:

以上是关于使用 Swift 访问 Shippo API 返回未记录的错误的主要内容,如果未能解决你的问题,请参考以下文章

Shippo API 导入错误

在 Shippo 中创建 Shipment 对象会返回处于 Queued 状态的对象

访问 https json api 每次都返回 nil (Swift)

Shippo:在其他函数中使用生成的交易的 label_url 变量

Shippo - Python 包异步问题

将哪些数据发布到 shippo webhook?