【发布时间】:2017-04-08 20:16:32
【问题描述】:
我正在尝试在我的 swift 应用程序中包含 Zoho Books API。 我想使用 POST 方法创建一个项目,但我一直收到一条错误消息,提示我的 JSONString 错误。
这是使用 POST 方法发送的 zoho 文档请求,以便创建和项目:
https://books.zoho.com/api/v3/items
?authtoken=e07119171812c29b3a0dacdb79a57e3f
&organization_id=10234695
&JSONString={
"name": "Hard Drive",
"description": "500GB, USB 2.0 interface 1400 rpm, protective hard case.",
"rate": 120.00,
"account_id": "460000000000388",
"tax_id": "460000000027005"
}
这是我做的。我感觉我的 JSONString 不符合预期的格式...
let postData = [
"authtoken" : "ZZZZZZZZZZZZZZZZZZZZZZ",
"organization_id" : "XXXXXXXXXXX",
"JSONString" :
[
"name": "Hard Drive",
"description": "500GB, USB 2.0 interface 1400 rpm, protective hard case.",
"rate": 120.00,
"account_id": "460000000000388",
"tax_id": "460000000027005"
]
] as [String : Any]
var request = URLRequest(url: NSURL(string:"https://books.zoho.eu/api/v3/items")! as URL)
request.httpMethod = "POST"
request.httpBody = try! JSONSerialization.data(withJSONObject: postData, options: [])
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
Alamofire.request(request).responseJSON
{ response in
if let JSON = response.result.value {
let zohoResponse: [String: AnyObject] = JSON as! [String : AnyObject]
print(zohoResponse)
}
}
答案是:
["code": 4, "message": Invalid value passed for JSONString]
我可以很好地发出 GET 请求,所以我相信在我的 swift 应用程序中使用 POST 方法也应该是可以管理的。
非常感谢您的帮助
问候
【问题讨论】: