'Unable to convert string to URL in Swift iOS

In order to make HTTP request call using URLSession,I need to convert string to URL first so I have tried everything but just can’t convert this piece of string to URL(string: ). This is the string: “http://api-aws-eu-qa-1.abc-cde.com/v1/car-types/manufacturer?page=0&pageSize=10&wa_key=abc-cde-efg-44ccd99”

I can’t share the exact Url but format is all same. The actual url with same format works fine in browser but no way in Swift, also that works fine in POSTMAN I have also tried URLComponents which hepls me create the URL from components to URL but that fails with 400 status code response error. I really appreciate the help, I am completely bogged down with this isuue and can’t proceed with my assignment.

Update: Tested with fiddler and request was not going through with this URL - "“http://api-aws-eu-qa-1.abc-cde.com%E2%80%8B/v1/car-types/manufacturer?page=0&pageSize=10&wa_key=abc-cde-efg-44ccd99”"

But when removed this %E2%80%8B in fiddler and resend it worked. Any thoughts ..?



Solution 1:[1]

String to URL

let url = URL(string: "the url string goes here")

URL to String

let url = URL(string: "the url string goes here")
let urlString = url.absoluteString

Creating URL from URLComponents

var url: URL? {
    var components = URLComponents()
    components.scheme = "https"
    components.host = "domain goes here" //example: twitter.com
    components.path = "/path/goes/here"
    components.queryItems = [
        URLQueryItem(name: "item1", value: string1),
        URLQueryItem(name: "item2", value: string2)
    ]

    return components.url
}

Solution 2:[2]

try to add HEADER to you request:

let url = URL(string : urlString)
var request = URLRequest(url : url)
request.setValue("Application/json", forHTTPHeaderField : "Content-Type")

Solution 3:[3]

Your URL is:

http://api-aws-eu-qa-1.abc-cde.com%E2%80%8B/v1/car-types/manufacturer?page=0&pageSize=10&wa_key=abc-cde-efg-44ccd99`

The "host" portion is:

api-aws-eu-qa-1.abc-cde.com%E2%80%8B

%E2%80%8B encodes a ZERO WIDTH SPACE. This is a perfectly valid URL:

URL(string: "http://api-aws-eu-qa-1.abc-cde.com%E2%80%8B/v1/car-types/manufacturer?page=0&pageSize=10&wa_key=abc-cde-efg-44ccd99")
=> Optional(http://api-aws-eu-qa-1.abc-cde.com%E2%80%8B/v1/car-types/manufacturer?page=0&pageSize=10&wa_key=abc-cde-efg-44ccd99)

However, there is no such host as api-aws-eu-qa-1.abc-cde.com%E2%80%8B, so you should expect the actual connection to fail.

I expect that whatever is generating your URL strings has a bug.

If you are having trouble creating the URL itself (if URL(string:) return nil, then I expect this is not your actual URL string.

If you construct an URLComponents, the results are correct but may be slightly misleading:

URLComponents(string: "http://api-aws-eu-qa-1.abc-cde.com%E2%80%8B/v1/car-types/manufacturer?page=0&pageSize=10&wa_key=abc-cde-efg-44ccd99")?.host
Optional("api-aws-eu-qa-1.abc-cde.com")

While this looks like "api-aws-eu-qa-1.abc-cde.com", the string actually has a ZERO WIDTH SPACE at the end, which is invisible (but still part of the host field, which will correctly fail if you try to connect to it).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Euan Traynor
Solution 2 AdR
Solution 3