enter image description here enter image description here在此处输入图片说明我正在使用Alamofire。我被困在张贴帖子请求。 我有一个帖子正文 -
[
{
"siteName": "lab1",
"locationCode": "111",
"locationName": "test1"
}
]
我该如何拨打电话?我在做 -
let parameters: Parameters = [
"siteName": "lab",
"locationCode": "1156",
"locationName": "123test"
]
Alamofire.request(URLStr, method: .post, parameters: parameters , encoding: JSONEncoding.default, headers: headers).responseJSON { response in
print("Request: \(String(describing: response.request))") // original url request
print("Response: \(String(describing: response.response))") // http url response
print("Result: \(response.result)") // response serialization result
if let json = response.result.value {
print("JSON: \(json)") // serialized json response
sucessHandler(json)
}
if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
print("Data: \(utf8Text)") // original server data as UTF8 string
failureHandler(response.error)
}
}
答案 0 :(得分:2)
非常感谢你们。我找到了另一种方法。
让fileUrl = NSURL(string:URLStr)
var request = URLRequest(url:fileUrl as! URL )
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let values = [parameters]
request.httpBody = try! JSONSerialization.data(withJSONObject: values)
Alamofire.request(request)
.responseJSON { response in
// do whatever you want here
switch response.result {
case .failure(let error):
print(error)
if let data = response.data, let responseString = String(data: data, encoding: .utf8) {
print(responseString)
failureHandler(response.error)
}
case .success(let responseObject):
print(responseObject)
do {
let user = try IeroLocationSave(JSONDecoder(response.data ?? "nil..12"))
//print("city is: \(user.address.city)")
sucessHandler(user)
//That's it! The object has all the appropriate properties mapped.
} catch {
print("unable to parse the JSON")
}
}
}
答案 1 :(得分:0)
let parameters: Parameters = ["feedback_name": "SwiftTest","feedback_email":"m@m.com","feedback_description":"Test"]
Alamofire.request("http://212.69.45.77:8082/api/feedbackapp",method: .post,parameters: parameters).responseJSON { response in
print("Request: \(String(describing: response.request))") // original url request
print("Response: \(String(describing: response.response))") // http url response
print("Result: \(response.result)") // response serialization result
if let json = response.result.value {
print("JSON: \(json)") // serialized json response
}
if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
print("Data: \(utf8Text)") // original server data as UTF8 string
}
}