1

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)

    }
jwarris91
  • 902
  • 9
  • 24

2 Answers2

1

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
gcharita
  • 7,729
  • 3
  • 20
  • 37
  • Ive updated the code in the question to better illustrate the issue, ive hard coded the auth header into the array with a sample token, it still istn present in the network monitor – jwarris91 Jul 02 '21 at 13:16
  • 1
    this was one issue, another was i missed a trailing / which seemed to remvoe the auth header..go figure – jwarris91 Jul 02 '21 at 13:28
0

Another solution is to use KeyValue pair as follows:

var header: HTTPHeaders = [:]
        
if let token = getValueFromUserDefaults(keyName: "authToken") as? String {
                
    header["Authorization"] = token
                
}
            

I always use this method. It's more handy for me.