shahin ali agharia
shahin ali agharia

Reputation: 1639

How to use header key from the api to authenticate URL in iOS Swift?

I am using Alamofire for the HTTP networking in my app. But in my api which is written in python have an header key for getting request, if there is a key then only give response. Now I want to use that header key in my iOS app with Alamofire, I am not getting it how to implement. Below is my code of normal without any key implementation:

Alamofire.request(.GET,"http://name/user_data/\(userName)@someURL.com").responseJSON { response in // 1
        print(response.request)  // original URL request
        print(response.response) // URL response
        print(response.data)     // server data
        print(response.result) 
}

I have a key as "appkey" and value as a "test" in my api. If anyone can help. Thank you!

Upvotes: 0

Views: 731

Answers (2)

Suhit Patil
Suhit Patil

Reputation: 12023

let headers: HTTPHeaders = [
    "Accept": "application/json",
    "appkey": "test"
]

Alamofire.request("http://name/user_data/\(userName)@someURL.com", headers: headers).responseJSON { response in
    print(response.request)  // original URL request
    print(response.response) // URL response
    print(response.data)     // server data
    print(response.result) 
}

Upvotes: 0

Naveen Ramanathan
Naveen Ramanathan

Reputation: 2206

This should work

let headers = [
            "appkey": "test"
        ]
        Alamofire.request(.GET, "http://name/user_data/\(userName)@someURL.com", parameters: nil, encoding: .URL, headers: headers).responseJSON {
            response in
            //handle response
        }

Upvotes: 1

Related Questions