How to send a POST request through Swift?

前端 未结 3 1885
面向向阳花
面向向阳花 2020-12-25 08:42

I have my controller like this -

def create
   if (@user = User.find_by_email(params[:email])) && @user.valid_password?(params[:password])
      ren         


        
相关标签:
3条回答
  • 2020-12-25 09:01

    I think you should pass your request instead of the url to session.dataTask

    here is how my code looks like:

    private let url = URL(string: "http://example.com/")!
    
    func httpPost(jsonData: Data) {
        if !jsonData.isEmpty {
            var request = URLRequest(url: url)
            request.httpMethod = "POST"
            request.httpBody = jsonData
    
            URLSession.shared.getAllTasks { (openTasks: [URLSessionTask]) in
                NSLog("open tasks: \(openTasks)")
            }
    
            let task = URLSession.shared.dataTask(with: request, completionHandler: { (responseData: Data?, response: URLResponse?, error: Error?) in
                NSLog("\(response)")
            })
            task.resume()
        }
    }
    
    0 讨论(0)
  • 2020-12-25 09:03

    I have made a Custom HTTP class where we can sent url, parameter and we will get Data from API. Below is the class.

    import Foundation
    
    //HTTP Methods
    enum HttpMethod : String {
       case  GET
       case  POST
       case  DELETE
       case  PUT
    }
    
    
    class HttpClientApi: NSObject{
    
    //TODO: remove app transport security arbitary constant from info.plist file once we get API's
     var request : URLRequest?
     var session : URLSession?
    
    static func instance() ->  HttpClientApi{
    
        return HttpClientApi()
    }
    
    
    
    func makeAPICall(url: String,params: Dictionary<String, Any>?, method: HttpMethod, success:@escaping ( Data? ,HTTPURLResponse?  , NSError? ) -> Void, failure: @escaping ( Data? ,HTTPURLResponse?  , NSError? )-> Void) {
    
         request = URLRequest(url: URL(string: url)!)
    
        logging.print("URL = \(url)")
    
        if let params = params {
    
    
            let  jsonData = try? JSONSerialization.data(withJSONObject: params, options: .prettyPrinted)
    
            request?.setValue("application/json", forHTTPHeaderField: "Content-Type")
            request?.httpBody = jsonData//?.base64EncodedData()
    
    
            //paramString.data(using: String.Encoding.utf8)
        }
        request?.httpMethod = method.rawValue
    
    
        let configuration = URLSessionConfiguration.default
    
        configuration.timeoutIntervalForRequest = 30
        configuration.timeoutIntervalForResource = 30
    
        session = URLSession(configuration: configuration)
        //session?.configuration.timeoutIntervalForResource = 5
        //session?.configuration.timeoutIntervalForRequest = 5
    
        session?.dataTask(with: request! as URLRequest) { (data, response, error) -> Void in
    
            if let data = data {
    
                if let response = response as? HTTPURLResponse, 200...299 ~= response.statusCode {
                    success(data , response , error as? NSError)
                } else {
                    failure(data , response as? HTTPURLResponse, error as? NSError)
                }
            }else {
    
                failure(data , response as? HTTPURLResponse, error as? NSError)
    
            }
            }.resume()
    
      }
    
    }
    

    Now you can refer below code to get how to make an API call.

      var paramsDictionary = [String:Any]()
    
        paramsDictionary["username"] = "BBB"
        paramsDictionary["password"]    = "refef"
    
        HttpClientApi.instance().makeAPICall(url: "Your URL", params:paramsDictionary, method: .POST, success: { (data, response, error) in
    
            // API call is Successfull
    
        }, failure: { (data, response, error) in
    
            // API call Failure
    
        })
    
    0 讨论(0)
  • 2020-12-25 09:06

    Here is the Example of POST API for calling Login API with parameters "emailaddress" and "password" with userEmailID and Userpassword as two strings holding values for email and password respectively.

    You can call this POST API anywhere in your view controller, as given below:

    self.postLoginCall(url: "Your post method url") example: self.postLoginCall(url: "http://1.0.0.1/api/login.php")

    func postLoginCall(url : String){
    
    
        let request = NSMutableURLRequest(url: NSURL(string: url)! as URL)
        request.httpMethod = "POST"
        let postString = "emailaddress=\(userEmailID!)&password=\(Userpassword!)"
        print(postString)
        request.setValue("application/x-www-form-urlencoded; charset=utf-8", forHTTPHeaderField: "Content-Type")
        request.httpBody = postString.data(using: String.Encoding.utf8)
    
        let task = URLSession.shared.dataTask(with: request as URLRequest) { data, response, error in
            guard error == nil && data != nil else {                                                          // check for fundamental networking error
                print("error=\(error)")
                return
            }
    
            do {
            if let responseJSON = try JSONSerialization.jsonObject(with: data!) as? [String:AnyObject]{
                print(responseJSON)
                print(responseJSON["status"]!)
    
    
                self.response1 = responseJSON["status"]! as! Int
    
                print(self.response1)
    
                //Check response from the sever
                if self.response1 == 200
                {
                    OperationQueue.main.addOperation {
    
                        //API call Successful and can perform other operatios
                       print("Login Successful")
                    }
    
                }
    
                else
                {
                    OperationQueue.main.addOperation {
    
                        //API call failed and perform other operations
                        print("Login Failed")
    
                    }
    
                }
    
    
            }
            }
            catch {
                print("Error -> \(error)")
            }
    
    
    
        }
    
    
        task.resume()
    
    
    
    }
    
    0 讨论(0)
提交回复
热议问题