POST request with data in body with Alamofire 4

前端 未结 4 692
闹比i
闹比i 2020-12-05 07:13

how is it possible to send a POST request with a data in the HTTP body with Alamofire 4? I used custom encoding at swift 2.3 it was working good. I converted my code swift 3

4条回答
  •  广开言路
    2020-12-05 07:38

    You need to send request like below in swift 3

    let urlString = "https://httpbin.org/get"
    
    Alamofire.request(urlString, method: .post, parameters: ["foo": "bar"],encoding: JSONEncoding.default, headers: nil).responseJSON {  
    response in
      switch response.result {
                    case .success:
                        print(response)
    
                        break
                    case .failure(let error):
    
                        print(error)
                    }
    }
    

    Swift 5 with Alamofire 5:

    AF.request(URL.init(string: url)!, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseJSON { (response) in
            print(response.result)
    
            switch response.result {
    
            case .success(_):
                if let json = response.value
                {
                    successHandler((json as! [String:AnyObject]))
                }
                break
            case .failure(let error):
                failureHandler([error as Error])
                break
            }
        }
    

提交回复
热议问题