Search code examples
swift3alamofire

How to add authorization on header using Swift 3.0 + Alamofire 4.0


Am trying to add authorization to my header using a return apikey of my login user am trying like this but it seems to not working because am getting an error that apikey is missing (is a return error from the webservices)

 func getFavourites ()
{

    let headers = ["Content-Type": "application/x-www-form-urlencoded", "authorization" : user.apiKey ]

    Alamofire.request("http://localhost:8888/food_pin/v1/restaurantToFavourites", method: HTTPMethod.get,  encoding: URLEncoding.default, headers: headers).responseJSON { (response:DataResponse<Any>) in


        if response.result.value != nil
        {
            print(response.result.value!)
            print(response.response!)
            self.returnAllFavourites(data: response.data!)
        }

        else
        {
            print(response.result.error!)
        }
    }

}

Solution

  • Use Authorization as your header key.

    However, you shouldn't need to do it, considering HTTP header keys are case-insensitive, the issue is probably on the server.