0
votes

My url is http://sample_domain/sample.json?{ "language" : "en", "category" : "sports"}

I am converting this to NSURL object like below.

NSURL *url=[NSURL URLWithString:@"http://sample_domain/sample.json?{ "language" : "en", "category" : "sports"}"];

its returning nil to NSURL object, i know the URL should be in specific format from apple documentation,otherwise it will be considered as a malware site and returns nil object,

my question is how can i pass these paremeters with apple standards, so that it will not retun nil object.

Thanks in advance.

1
Can you encode the data differently? It might make more sense to use normal query encoding, e.g.: http://sample_domain/sample.json?language=en&category=sports As you can see, it's still the same information but now it's working much more with the way the normal web works instead of insisting on doing it differently. - Donal Fellows
Hi Donald,if i try this even in browser i am not getting response, but if i do like below NSString *str1=[@"\"lan\" : \"en\", \"cat\" : \"menus\" " stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSString *str2=[NSString stringWithFormat:@"domain/test.json?{%@}",str1]; NSURL *sample=[NSURL URLWithString:str2]; now i am getting nil url object, but if i copy and paste the 'str2' in browser i am getting response data, so here i should not encode '{' aswell NSURL should not be null, any ideas please. - mac

1 Answers

1
votes

Try

NSURL *url=[NSURL URLWithString:[@"http://sample_domain/sample.json?{ \"language\" : \"en\", \"category\" : \"sports\"}"
                  stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

I'm not sure why '"' characters are not escaped in your example, but I think without escapes your code should not compile.

Also you need (and that's the main problem) to replace characters that are illegal in url (e.g. ':' with corresponding percent escapes - that's what stringByAddingPercentEscapesUsingEncoding function does.