Skip to content

Instantly share code, notes, and snippets.

@exclusiveTanim
Created March 25, 2020 13:54
Show Gist options
  • Save exclusiveTanim/93bc4a3431e952e84f6e075ba7010438 to your computer and use it in GitHub Desktop.
Save exclusiveTanim/93bc4a3431e952e84f6e075ba7010438 to your computer and use it in GitHub Desktop.
Just use this function for getting JSON
func userResponse(){
// Prepare URL
let url = URL(string: "http://ba.pakizatvl.com:8070/CRAPI.asmx/GetUserLogin")
guard let requestUrl = url else { fatalError() }
// Prepare URL Request Object
var request = URLRequest(url: requestUrl)
request.httpMethod = "POST"
// HTTP Request Parameters which will be sent in HTTP Request Body
let postString = "UId=rupun&UPas=Rupun";
// Set HTTP Request Body
request.httpBody = postString.data(using: String.Encoding.utf8);
// Perform HTTP Request
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
// Check for Error
if let error = error {
print("Error took place \(error)")
return
}
// Convert HTTP Response Data to a String
if let data = data, let dataString = String(data: data, encoding: .utf8) {
//print("Response data string:\n \(dataString)")
let replaced = dataString.replacingOccurrences(of: "<?xml version=\"1.0\" encoding=\"utf-8\"?>", with: "")
let replaced2 = replaced.replacingOccurrences(of: "<string xmlns=\"http://172.16.61.201:8070/\">", with: "")
let replaced3 = replaced2.replacingOccurrences(of: "</string>", with: "")
print("Response data string: \(replaced3)")
if let data = replaced3.data(using: String.Encoding.utf8) {
do {
let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String:Any]
if let userResponse = json?["LogStatus"] {
print("The response is at \(userResponse)")
} else {
print("Couldn't find the user")
}
} catch {
print("Something went wrong")
}
}
}
}
task.resume()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment