iosswiftalamofire

Add GET parameter in request adapter of Alamofire


I am trying in the Request Adapter of Alamofire to add a GET parameter. However in the request adapter I am only able to add HTTPHeader fields.

Currently my request adapter looks like:

// MARK: - RequestAdapter

func adapt(_ urlRequest: URLRequest) throws -> URLRequest {
    if let url = urlRequest.url, url.lastPathComponent.hasPrefix(baseURLString) {
        var urlRequest = urlRequest

        // Want to inject param here
        // e.g. urlRequest.addParam(param: "session", value: sessionToken")

        return urlRequest
    }

    return urlRequest
}

I have a Router configured for the Paths but since I want my AuthHandler to be responsible to all Authentication related stuff I want to inject my sessionToken. This makes sure, together with RequestRetrier that any HTTP 401 related error is dealt with.

What is the best way to change the urlRequest?


Solution

  • Can you try

    let params: Parameters = ["session": sessionToken]
    return URLEncoding.default.encode(urlRequest, with: params)
    

    (or)

    return URLEncoding.queryString.encode(urlRequest, with: params)
    

    Thanks Sriram