Search code examples
iosswiftalamofireurlrequest

Authorization Header not setting in Alamofire 5?


I am setting the basic auth token to my URLRequest and using Alamofire to execute.

I set 3 headers, content, accept and auth...content and accept are visible in the network traffic but auth is not. It is available if i print the headers of the URLRequest before its sent...

Any ideas here? as i dont see how its removing the auth header before posting

Code as follows:

        // Construct url
        let url = try APIConstants.baseUrl.asURL()

        // Append path
        var urlRequest = URLRequest(url: url.appendingPathComponent(path))

        // Determine HTTP method
        urlRequest.httpMethod = method.rawValue

        let headers: HTTPHeaders = [
            .contentType(APIConstants.ContentType.json.rawValue),
            .accept(APIConstants.ContentType.json.rawValue),
        ]

        if let token = token {
            urlRequest.addValue("\(APIConstants.API.token.rawValue) \(token.key)",
                                forHTTPHeaderField: "Authorization")
        }

        urlRequest.headers = headers

        // Add http body to request
        if let parameters = parameters {
            do {
                let data = try JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted)
                urlRequest.httpBody = data
            } catch (_) {
                print("APIRouter: Failed to parse body into request.")
            }
        }

        //Encoding
        let encoding: ParameterEncoding = {
            switch method {
            case .get:
                return URLEncoding.default
            default:
                return JSONEncoding.default
            }
        }()

        return try encoding.encode(urlRequest, with: parameters)

    }

In my rest client i execute like this:

        return Observable<T>.create { observer in
            let request = AF.request(urlConvertible).responseDecodable { (response: DataResponse<T, AFError>) in
                switch response.result {
                case .success(let value):
                    observer.onNext(value)
                    observer.onCompleted()
                case .failure(let error):
                    switch response.response?.statusCode {
                    case 403:
                        observer.onError(APIError.forbidden)
                    case 404:
                        observer.onError(APIError.notFound)
                    case 409:
                        observer.onError(APIError.conflict)
                    case 500:
                        observer.onError(APIError.internalServerError)
                    default:
                        observer.onError(error)
                    }
                }
            }

            return Disposables.create {
                request.cancel()
            }
        }
    }

Edit:

Updated func to show further issue:

    func asURLRequest() throws -> URLRequest {
        // Construct url
        let url = try APIConstants.baseUrl.asURL()

        // Append path
        var urlRequest = URLRequest(url: url.appendingPathComponent(path))

        // Determine HTTP method
        urlRequest.httpMethod = method.rawValue

        let headers: HTTPHeaders = [
            .contentType(APIConstants.ContentType.json.rawValue),
            .accept(APIConstants.ContentType.json.rawValue),
            .authorization("Token a5555485aa251b28fdsfasdfdsb379c131fddad")
        ]

        urlRequest.headers = headers

        // Add http body to request
        if let parameters = parameters {
            do {
                let data = try JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted)
                urlRequest.httpBody = data
            } catch (_) {
                print("APIRouter: Failed to parse body into request.")
            }
        }

        //Encoding
        let encoding: ParameterEncoding = {
            switch method {
            case .get:
                return URLEncoding.default
            default:
                return JSONEncoding.default
            }
        }()

        return try encoding.encode(urlRequest, with: parameters)

    }

Solution

  • After setting the Authorization header in your URLRequest:

    urlRequest.addValue("\(APIConstants.API.token.rawValue) \(token.key)", forHTTPHeaderField: "Authorization")
    

    then you override all the request's headers by setting headers property:

    urlRequest.headers = headers
    

    A nice solution would be to update the headers that you already created above, like this:

    var headers: HTTPHeaders = [
        .contentType(APIConstants.ContentType.json.rawValue),
        .accept(APIConstants.ContentType.json.rawValue),
    ]
    
    if let token = token {
        headers.add(.authorization("\(APIConstants.API.token.rawValue) \(token.key)"))
    }
    
    urlRequest.headers = headers