Skip to content

Instantly share code, notes, and snippets.

@randhirraj3130
Created April 18, 2018 12:49
Show Gist options
  • Save randhirraj3130/2b8ecc7e15bb9c93868d98de554fcbf9 to your computer and use it in GitHub Desktop.
Save randhirraj3130/2b8ecc7e15bb9c93868d98de554fcbf9 to your computer and use it in GitHub Desktop.
How to get and post data using Alamofire
// GET the JSON Data like this
Alamofire.request("YOUR-SERVER-URL").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 resul
if let json = response.result.value{
let JsonData = json as! NSDictionary
let status = JsonData["status"] as? Int
let message = JsonData["message"] as? String
print("status \(String(describing: status))")
print("message \(String(describing: message))")
print("JSON data \(JsonData)")
}
}
// POST the data using Alamofire
let prs = [
"email": "james@gmail.com",
"password" :"iws123#",
"user_type" : "2"
] as [String:String]
Alamofire.request("YOUR-SERVER-URL", method: .post, parameters: prs,encoding: JSONEncoding.default, headers: nil).responseJSON {
response in
switch response.result {
case .success:
let JsonData = response.result.value as! NSDictionary
let message = JsonData["message"] as? String
let status = JsonData["status"] as? Int
print("message \(String(describing: message))")
print("status \(String(describing: status))")
print("responce \(String(describing: response.result.value))")
break
case .failure(let error):
print(error)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment