How to send a POST request through Swift?

前端 未结 3 1931
面向向阳花
面向向阳花 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()
        }
    }
    

提交回复
热议问题