Lyova Kurginyan Lyova Kurginyan - 29 days ago 23
iOS Question

Alamofire Router that returns POST request

after reading RAYWENDERLICH Alamofire tutorial I decided to build own router, It works fine except a little detail. It returns

GET
requests but I need
POST
request in my case. I spend some time trying to set
HTTPMethod = "POST"
in Router but probably because of lack of experience I was doing it wrong and getting some errors. So what exactly do I need to change in router to make it return
POST
requests?

Her is my code

enum Router: URLRequestConvertible
{
static let baseURLString = "http://www.com"
case Friends(Int)
var URLRequest: NSURLRequest
{
let (path: String, parameters: [String: AnyObject]) = {
switch self
{
case .Friends (let ID):
let params = ["userID": "\(ID)"]
return ("/getFriends.php", params)
}
}()
let URL = NSURL(string: Router.baseURLString)
let URLRequest = NSURLRequest(URL: URL!.URLByAppendingPathComponent(path))
let encoding = Alamofire.ParameterEncoding.URL
return encoding.encode(URLRequest, parameters: parameters).0
}
}

Answer

Following code perfectly :) Thanks @Kaan Dedeoglu

  enum Router: URLRequestConvertible
  {
    static let baseURLString = "http://www.com"
    case Friends(Int)
    var URLRequest: NSURLRequest
      {
        let (path: String, parameters: [String: AnyObject]) = {
          switch self
          {
          case .Friends (let ID):
            let params = ["userID": "\(ID)"]
            return ("/getFriends.php", params)
          }
          }()
        let URL = NSURL(string: Router.baseURLString)
        let URLRequest = NSMutableURLRequest(URL: URL!.URLByAppendingPathComponent(path))
        URLRequest.HTTPMethod = "POST"
        let encoding = Alamofire.ParameterEncoding.URL
        return encoding.encode(URLRequest, parameters: parameters).0
    }
  }
Comments