fatihyildizhan
fatihyildizhan

Reputation: 8832

Alamofire 2.0 and Swift 2 - Header is not working. See how to fix it

When I upgraded my project to swift 2 with Alamofire 2 headers stopped working without any errors in code. The reason is that headers are not working old style.

 // login with Alamofire 1 and Swift 1.2 - WITH HEADER
 func loginAlamofire_1(username:String) {
            manager.session.configuration.HTTPAdditionalHeaders = ["Authorization": "yourToken"]
            manager.request(.POST, "login_url", parameters: ["username" : username], encoding: ParameterEncoding.JSON)
                .response{ (request, response, data, error) -> Void in
                    if error != nil{
                        print("error!")
                    } else {
                        print("welcome")
                    }
            }
        }

You can see the fixed version below

Upvotes: 0

Views: 1313

Answers (1)

fatihyildizhan
fatihyildizhan

Reputation: 8832

You can fix the header problem by sending headers in requests.

// login with Alamofire 2.0 and Swift 2.0 - WITH HEADER
func loginAlamofire_2(username:String) {
    manager.request(.POST, "login_url", parameters: ["username" : username], encoding: ParameterEncoding.JSON, headers: ["Authorization": "yourToken"])
        .response{ (request, response, data, error) -> Void in
            if error != nil{
                print("error!")
            } else {
                print("welcome")
            }
    }
}

Upvotes: 1

Related Questions