如何在Swift 5上附加“ Postman 操作”?我想使用此代码登录ios(iphone)上的rest服务。
下面是Post
Method 的代码,使用URLSession
let Url = String(format: "http://10.10.10.53:8080/sahambl/rest/sahamblsrv/userlogin")
guard let serviceUrl = URL(string: Url) else { return }
let parameters: [String: Any] = [
"request": [
"xusercode" : "YOUR USERCODE HERE",
"xpassword": "YOUR PASSWORD HERE"
]
]
var request = URLRequest(url: serviceUrl)
request.httpMethod = "POST"
request.setValue("Application/json", forHTTPHeaderField: "Content-Type")
guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: []) else {
return
}
request.httpBody = httpBody
request.timeoutInterval = 20
let session = URLSession.shared
session.dataTask(with: request) { (data, response, error) in
if let response = response {
print(response)
}
if let data = data {
do {
let json = try JSONSerialization.jsonObject(with: data, options: [])
print(json)
} catch {
print(error)
}
}
}.resume()
}
嗨,奥斯汀,它对我有用,非常感谢。
很高兴知道,它为您工作。请接受它作为正确答案
奥斯汀,您好,print(json)结果为{响应= {成功= 1; successmsg =“成功连接”; };}我如何获得“成功”的价值?
使用此库处理JSON,[ github.com/SwiftyJSON/SwiftyJSON]