-9

I am new in iOS development . I am using alamofire in swift 3 . How can i send post request in alamofire . It also gives extra argument in method.

Thanks in advance

Barak
  • 535
  • 6
  • 18
  • 1
    Have you looked in to [this?](http://stackoverflow.com/questions/27855319/post-request-with-a-simple-string-in-body-with-alamofire) – Barak Apr 10 '17 at 05:43

3 Answers3

2

First of all you add almofire library into your project then import almofire into your ViewController then below method apply in your button action.

func webServiceLogin(isFbLogin:Bool,email:String,password:String)
        {
            var parameters:[String:String]?

             parameters = ["hash":email as String,"key":password ]

            Alamofire.request("your url", method: .post, parameters: parameters,encoding: URLEncoding.default, headers: nil).responseJSON {
                response in
                hideHud(self.view)
                switch response.result {
                case .success:
                    if let dictSuccess:NSDictionary =  response.value as! NSDictionary?
                    {

                       }

                    break
                case .failure(let error):
                    Alert.showAlertWithTitle(strTitle: appTitle, strMessage: error.localizedDescription, onView: self)
                    print(response)
                    print(error)
                }

            }
        }
Vinod Kumar
  • 3,375
  • 1
  • 17
  • 35
0

Use like bellow and pass your parameter which you want to send in server. Better you write an Network layer class using this then It will be reusable throughout the whole application.

 static func serverRequest(urlString: URL?, Parameter:NSDictionary?, completion: @escaping (_ serverResponse: AnyObject?,_ error:NSError?)->()){
        // let parameters: Parameters = ["foo": "bar"]
        //let headers = ["Authorization": "123456"]
        Alamofire.request(urlString!, parameters:nil, headers: nil).responseJSON { response in

            if(response.result.value != nil){
                let serverResponse = JSON(response.result.value!)
                //print("Array value is \(serverResponse.arrayValue)")

                completion(serverResponse as AnyObject?, nil)
            }
            else{
                completion(nil, response.result.error as NSError?)
            }
        }

    }
Aashish1aug
  • 795
  • 1
  • 8
  • 22
0

You can use alamofire manager

    var alamoFireManager = Alamofire.SessionManager
    let request = URLRequest(url:_yourULR)
    request.HTTPMethod = requestMethod.rawValue
    request.timeoutInterval = //set yours
    request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
    request.addValue("application/json", forHTTPHeaderField: "Accept")
    request.addValue("gzip", forHTTPHeaderField: "Accept-Encoding")

    request.HTTPBody = "you_bodydataSTring".dataUsingEncoding(String.Ecoding.utf8)

    alamoFireManager.request(request)
        .validate()
        .responseString { (response) -> Void in
            let datastring = NSString(data:response.data!, encoding: String.Ecoding.utf8)
            switch response.result {
            case .Success:

                if response.response?.statusCode == 200 {
                   //code for success
                }else{
                   //others
                }

            case .Failure(let error):
               //request failed
            }

    }
}