Skip to main content

Posts

Showing posts from March, 2023

How to Call Post API Using URLSession And URLRequest in Swift

  Call Post API Using URLSession & URLRequest in Swift import UIKit import PlaygroundSupport PlaygroundPage. current . needsIndefiniteExecution = true struct APIURL EndPoint { static let registerUser = " https://api-dev-scus-demo.azurewebsites.net/api/User/RegisterUser " static let getUser = " https://api-dev-scus-demo.azurewebsites.net/api/User/GetUser " } struct User { func registerUser () { // code to register user var urlRequest = URLRequest ( url : URL ( string : APIURL EndPoint . registerUser ) ! ) urlRequest. httpMethod = " post " let dataDictionary = [ " name " : " codecat15 " , " email " : " codecat15@gmail.com " , " password " : " 1234 " ] do { let requestBody = try JSONSerialization. data ( withJSONObject : dataDictionary, options : . prettyPrinted ) urlRequest. ...

How to Call API Using URLSession in Swift

  Call  API Using URLSession In Swift  This is  Basic way to Call API in swift . 1- Create swift playground file like ApiCall.playground . ApiCall is name of file and .playground is extension of file. 2- Write the Code into this file ApiCall.playground import UIKit import PlaygroundSupport PlaygroundPage . current . needsIndefiniteExecution = true //this API Call Function func getAPIDataFromServer (){ let session = URLSession . shared let serviceUrl = URL ( string : "https://jsonplaceholder.typicode.com/todos/1" ) let task = session . dataTask ( with : serviceUrl !) { ( serviceData , serviceResponse , error ) in if error == nil { let httpResponse = serviceResponse as ! HTTPURLResponse if ( httpResponse . statusCode == 200 ){ // data parse let jsonData = try ? JSONSerialization . jsonObject ( with : serviceData !, options : . mutableCont...