0

I'm looking a lot of examples, mainly from @matt on the web but I can't get this work:

import Alamofire

extension Request {
    public func debugLog() -> Self {
        debugPrint(self)
        return self
    }
}

func login(userName: String, passWord: String) -> String {
    let manager = Manager.sharedInstance
    // Specifying the Headers we need
    manager.session.configuration.HTTPAdditionalHeaders = [
        "Content-Type": "application/json",
        "User-Agent": "test",
        "Cache-Control": "no-cache",
        "Authorization": "apikey"
    ]
    // When

    let params =
        ["userName" : userName,
        "passWord" : passWord]

    Alamofire.request(.POST, Constants.apiURL.url + "users/login", parameters: params)
        .validate()
        .debugLog()
        .responseJSON { responseRequest, responseResponse, responseResult in
            NSLog(String(responseRequest)) // never enter here
            NSLog(String(responseResponse))
            NSLog(String(responseResult))
    }

    return ""
}

It never ever enter inside my block .responseJSON. Whatever I use as parameter, forcing an error or getting the perfect cURL command from debug, it never works.

My cURL is fine, I can execute it normally in the bash and I get a good json result.

curl -i \
    -X POST \
    -H "Content-Type: application/x-www-form-urlencoded" \
    -H "Cache-Control: no-cache" \
    -H "User-Agent: test" \
    -H "Authorization: apikey" \
    -H "Content-Type: application/json" \
    -d "passWord=123&userName=123" \
    "http://api.cc/users/login"
GIJOW
  • 2,307
  • 1
  • 17
  • 37

2 Answers2

0

Do you get any output in the console for this?

Looks like you are using the Xcode 7 with App Transport Security enabled for your app and sending an HTTP request. Try ignoring ATS restrictions by adding this to your Info.plist:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

More details here: How do I load an HTTP URL with App Transport Security enabled in iOS 9?

Community
  • 1
  • 1
filwag
  • 690
  • 5
  • 10
0

I think yo have a problem with headers. You should update your requests. You can add your headers in your request with Alamofire 2 and Swift 2.

For solution: go to the solution

Community
  • 1
  • 1
fatihyildizhan
  • 8,614
  • 7
  • 64
  • 88