4

I have this confusing error. I am sending JSON through GET method and a website will parse and display the data. The problem is I am getting the error "NSURLErrorDomain Code -1000" or more simply "Bad URL". The thing is when I check the server, the data I sent is successfully parsed and displayed. So I am really confused why am I getting this "bad URL" error at all. Can anyone helped me out?

Here is the error I am receiving:

Error Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0xff73df0 {NSUnderlyingError=0xff73810 "bad URL", NSLocalizedDescription=bad URL}

EDIT:

http://sample.com/imwebjson.php?sid=5amp13&job=sendNotes&im_flds={\"im_uid\":"1",\"im_bookid\":"57",\"im_pagenr\":"1",\"im_notes\":"Testing%5C%5Cn"}

Ok you might ask why some parts of the JSON string is encoded already. These encoded parts are special characters. I realized that the stringByAddingPercentEscapesUsingEncoding is very incomplete. It doesn't encode all special characters, and what more is that when it encodes some special characters, the URL is not recognized at all. So I decided to manually encode the special characters into the string.

JeremyP
  • 84,577
  • 15
  • 123
  • 161
user1412469
  • 279
  • 5
  • 17
  • Is there other spaces or other special characters present in URL? – rishi May 28 '12 at 09:54
  • Check this out - http://stackoverflow.com/questions/6287230/bad-url-when-requesting-with-nsurl – rishi May 28 '12 at 09:54
  • I actually tried this already. It was my first assumption. I encoded the JSON file so that it will be a compatible if I placed it in the URL. However, it gave me another error: NSErrorFailingURLStringKey. Do you have any idea what it meant? – user1412469 May 28 '12 at 10:05
  • What is the actual URL you are using? – JeremyP May 28 '12 at 10:15
  • @JeremyP I can't give you the exact URL but here is a slightly modified one: http://mydomain.com/imwebjson.php?sid=5amp13&job=sendNotes&im_flds={\"im_uid\":"1",\"im_bookid\":"57",\"im_pagenr\":"1",\"im_notes\":"Testing%5C%5Cn"} This is a working URL. Meaning this URL is actually getting parsed by the server. Please remember that I am using a GET method here so the JSON is really part of the string. And like I've said I decided not to encode it since it actually fails to parse the data. – user1412469 May 28 '12 at 10:17
  • @user1412469: Please edit your question and add the URL there. I can't read it in the comment. – JeremyP May 28 '12 at 10:28
  • yes space(%20) is the reason for that ERROR [this link](https://stackoverflow.com/questions/25806235/error-domain-nsurlerrordomain-code-1000-bad-url-nsunderlyingerror-0xac926a0) helps you. – Mohamed Jaleel Nazir Sep 12 '14 at 10:42

2 Answers2

5

The colon character : (at least, possibly others like " and {) needs to be percent encoded in URLs.

JeremyP
  • 84,577
  • 15
  • 123
  • 161
  • I'll try it once we got the server running again. I'll keep you posted :) – user1412469 May 28 '12 at 11:16
  • I tried your suggestion but now it gave me 2 errors: NSErrorFailingURLKey (which happens when I attached the encoded JSON to the URL) and kCFErrorDomainCFNetwork Code=303. The data was parsed successfully, that's why I am getting confused with these errors. – user1412469 May 29 '12 at 01:29
  • Please disregard the KCFErrorDomainCFNetwork. I got the error because of a parsing issue :) – user1412469 May 29 '12 at 01:38
0

In the GET parameters you have to change spaces " " for "%20"

user2387149
  • 1,219
  • 16
  • 28