Unsupported URL in NSURLRequest
Asked Answered
S

11

39

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?

Selfwill answered 29/11, 2013 at 22:45 Comment(4)
Try adding http:// to the start of the URL.Exostosis
Add "http://" and try again? Also, do you request the port that way or via the URL Request (I'm not sure myself).Grethel
Agreed. Without the "http://" (or other URL prefix), it is not a URL at all.Coimbatore
make sure your server is running and then make sure the url prefiexRadiotherapy
M
62

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

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

Merth answered 30/11, 2013 at 4:57 Comment(2)
I have a URL starting with http://... and I still get the error. Anybody knows what else could cause this?Breviary
@最白目 in my case the problem was with the end of the url. I doubt you still need answer for this but it may help someone in the future :)Reduplicative
L
27

In my case I fixed it with this :

strURL = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

strURL contains the string with the URL.

Lustihood answered 26/9, 2015 at 22:16 Comment(0)
M
11

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

Merriemerrielle answered 17/5, 2016 at 8:1 Comment(0)
A
9

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.

Actuality answered 18/12, 2014 at 3:11 Comment(2)
Thanks a lot, you saved a dayShallow
you are welcome, if its helps you guys, can you rate up for my answer?Actuality
L
3

same answer as Alf G but with IOS 9+

strUrl = [strUrl stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLHostAllowedCharacterSet]
Lockhart answered 10/6, 2018 at 15:0 Comment(0)
K
2

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.

Kinshasa answered 11/6, 2015 at 8:4 Comment(0)
Y
2

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

Yoke answered 20/4, 2016 at 15:31 Comment(0)
B
1

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.

Beabeach answered 27/7, 2016 at 19:57 Comment(0)
S
0

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.

Servile answered 29/5, 2015 at 18:5 Comment(0)
G
0

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://mcmap.net/q/239911/-remove-whitespace-from-string-in-objective-c for code that will trim newline characters out of a string.

Greensboro answered 5/6, 2020 at 4:2 Comment(0)
S
0

I solved VIA below code

FinalUrl = [FinalUrl stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLQueryAllowedCharacterSet];

Thanks.

Salters answered 28/9, 2020 at 11:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.