ghostrider
ghostrider

Reputation: 5259

Unsupported URL in NSURLRequest

If I run this request from my terminal I can see the JSON requests as normally:

curl -XGET 192.168.0.6:8888/scripts/data/backend2/index.php/name/_all

My code for the NSURlRequest is this:

 NSURLRequest *request = [NSURLRequest requestWithURL:
                             [NSURL URLWithString:@"192.168.0.6:8888/scripts/data/backend2/index.php/name/_all"]];

    [[NSURLConnection alloc] initWithRequest:request delegate:self];

And I am getting this error:

didFailWithError
2013-11-29 22:31:08.164 Ski Greece[607:a0b] Connection failed: Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo=0xcd042d0 {NSErrorFailingURLStringKey=192.168.0.6:8888/scripts/data/backend2/index.php/name/_all, NSErrorFailingURLKey=192.168.0.6:8888/scripts/data/backend2/index.php/name/_all, NSLocalizedDescription=unsupported URL, NSUnderlyingError=0xdbdcc70 "unsupported URL"}

How can I make the call to that URL? I cannot access the server code - I know it is just setup to return me what I need, if I call that URL?

Upvotes: 40

Views: 72560

Answers (11)

Aaban Tariq Murtaza
Aaban Tariq Murtaza

Reputation: 1252

I solved VIA below code

FinalUrl = [FinalUrl stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLQueryAllowedCharacterSet];

Thanks.

Upvotes: 0

Jon Schneider
Jon Schneider

Reputation: 26983

This error can happen if your URL contains a trailing newline.

I ran into this when I was appending an API key read from a text file (to keep it out of the version control system) to the end of an URL, and the file included a training whitespace.

See https://stackoverflow.com/a/4645700/12484 for code that will trim newline characters out of a string.

Upvotes: 0

Aviv Frenkel
Aviv Frenkel

Reputation: 31

same answer as Alf G but with IOS 9+

strUrl = [strUrl stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLHostAllowedCharacterSet]

Upvotes: 3

Keshav
Keshav

Reputation: 3273

I too struggled for the same error, even though the url path was correct but there was a space in it, before http, like below:

NSString *path = @" http://www.mylink/";
NSURL *url = [NSURL URLWithString:path];

so I was getting url as nil and so it was giving "unsupported URL". then by removing the space worked for me.

Upvotes: 9

Sanchit Kumar Singh
Sanchit Kumar Singh

Reputation: 513

It seems its a malformed URL or it's not a valid url at all, try to hit this url in browser, I think you will not get any result. error code=-1002 occurs when the url is unsupported.

Upvotes: 1

halftrue
halftrue

Reputation: 149

In my case, I visit a service running on my own mac, so my url is 127.0.0.1:8080/list

After I add a http:// scheme. It works!

Now it is http://127.0.0.1:8080/list instead of 127.0.0.1:8080/list

Upvotes: 11

Justin Domnitz
Justin Domnitz

Reputation: 3307

In my case, I was passing an unwrapped optional in Swift. Once I unwrapped the optional to string, the URL was accepted correctly.

Upvotes: 2

Alf G
Alf G

Reputation: 271

In my case I fixed it with this :

strURL = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

strURL contains the string with the URL.

Upvotes: 27

Narasimha Nallamsetty
Narasimha Nallamsetty

Reputation: 1263

In my case spaces are added to my URL. I have removed spaces and run. Please make sure you have not added any spaces to your URL even when you are passing parameters. Hope it helps someone.

Upvotes: 2

ClintChil
ClintChil

Reputation: 35

Like stated before, a space in the URL can cause this but it's also possible that your string contains an unsupported character. For example, if you copy and paste a URL from a PDF, Word or other document it might contain unsupported characters. To the eye it looks fine but not the compiler.

To fix this, in your [NSURL URLWithString:@"http://blabhblabh"] method, delete the entire line of code, not just the url, and retype the link and method by hand.

Upvotes: 0

Vizllx
Vizllx

Reputation: 9246

Try to include appropriate url scheme to your url, e.g.

[NSURL URLWithString:@"http://www...

Upvotes: 63

Related Questions