Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alamofire encoding invalidURL

Good afternoon all, newbie here trying to learn Swift / Alamofire. I've struggled with this for almost two days and am ready to move on.

My URL appears to be created corrected, but I get an error when making the request. In looking at the Xcode debug logs, it looks like my single quotes are being escaped when the request is being sent.

let owner : String = "[email protected]"

func getDeploymentsByOwner(token: String, owner: String, success: @escaping (JSON) -> Void, failure: @escaping (Error) -> Void) {

    let headers = [
        "Authorization": "Bearer \(token)",
        ]
    print(owner)


    let url = "https://corp.local?$filter=owners/ref eq '\(owner)'"


    // created URL is https://corp.local?$filter=owners/ref eq '[email protected]'


    Alamofire.request(url, method: .get, encoding: URLEncoding.queryString, headers: headers).responseJSON { (responseObject) -> Void in
        if responseObject.result.isSuccess {
            let resJSON = JSON(responseObject.result.value!)
            success(resJSON)
        }
        if responseObject.result.isFailure {
            let error = responseObject.result.error
            print(error)
            failure(error!)
        }
    }

My error message looks like this // Xocde debug error

Optional(Alamofire.AFError.invalidURL("https//:corp.local?$filter=owners/ref eq \'[email protected]\'"))
invalidURL(https://corp.local?$filter=owners/ref eq \'[email protected]\'")

I Can get all the items without $filter, so the request works as long as the filter is omitted.

// Postman works with the generated URL, but the one with the backslashes () won't

like image 366
TryingToMakeItWork Avatar asked Sep 26 '17 21:09

TryingToMakeItWork


1 Answers

Thank you for your response, unfortunately it did not work. The entire URL got mangled including the hostname. But it did give me a starting point to figure it out, thank you. The working code is below

    let url = "https://corp.local?$filter=owners/ref eq '\(owner)'"
    let encodedUrl = url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
like image 88
TryingToMakeItWork Avatar answered Oct 20 '22 08:10

TryingToMakeItWork