I have a api which needs to use a url string in order to work (wouldn't work with the normal NSDictionary request). The string I'm trying to use is
http://10.1.10.25:8181/config?param={"obj":["hours"]}
However, the following code which I used to escape the characters does not work. It returns a bad url error. What is the proper way to escape characters here?
NSURL *url = [NSURL URLWithString:#"http://10.1.10.25:8181/config?param={\"obj\":[\"hours\"]}"];
// Error: Error Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0x8ca3780 {NSUnderlyingError=0x8ca6980 "bad URL", NSLocalizedDescription=bad URL}
Below is a picture of a working example using a online REST service.
In this case you just need to encode your param like this:
http://10.1.10.25:8181/config?param=%7B%22obj%22%3A%5B%22hours%22%5D%7D
Related
I created an API using AWS and generated the SDK for iOS use. However, when I try to call the API, I get this error.
Error occurred: Error
Domain=NSURLErrorDomain Code=-1002 "unsupported URL"
UserInfo={NSUnderlyingError=0x1546a36b0{Error Domain=kCFErrorDomainCFNetwork Code=-1002 "(null)"}, NSErrorFailingURLStringKey=(null)https://(api-path),...}
The URL is hard-coded in the SDK and does not have any sort of (null) in the string. I have no clue where this is coming from.
Replacing the following code in the AWS-generated init(configuration: AWSServiceConfiguration) worked for me:
Original:
if let endpoint = configuration.endpoint {
self.configuration.baseURL = endpoint.URL
}
Replace by:
self.configuration.baseURL = URL(string: URLString)
I don't understand why it says the endpoint is nil even though it has successfully been created immediately beforehand. Anyway, it might help you solve the issue.
Check your NSURL object to see if it is nil when you init it from NSString.
Then check your url in NSString to see if it contains any white spaces, which make your url invalid.
To solve that you have to add this to your url:
NSString *yourUrl; // Your url in NSString type.
NSString *encoded = [substring stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL *url = [NSURL URLWithString:encoded];
To those who downvoted my answer:
If you downvoted because it wasn't written in swift, I just tried my best to help.
Just because I don't have experience with swift programming, doesn't mean I have to stop helping others to solve their problems.
Am trying to hit a GET service with search keyword as "R+Co", provided in the URL. But the service is receiving as "R Co" which is affecting the search logic. Can we read this as '+' itself in the service?? Thanks in Advance!!
Edit : The service is called from iOS.
In iOS you can encode the url string into UTF8 as
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
Use URL Encoding while making GET request.
See link
Encode the query String parameter which is your search keyword.
URLEncoder.encode("R+Co","UTF-8");
This can be escaped using character set using ASCII Encoding Reference. For '+' we can use '%2B'. Now 'R+Co' is read as it is and the service is hit with 'R%2BCo'.
I am using AFNetworking to fetch data from the server.
When there is an accented character in my URL I get an error like this:
userInfo={"NSUnderlyingError"=>#<__NSCFError:0xfd3aa70,
description="bad URL", code=-1000, domain="kCFErrorDomainCFNetwork",
userInfo={"NSLocalizedDescription"=>"bad URL"}>,
"NSLocalizedDescription"=>"bad URL"}>
However, when I try the URL from a browser (chrome), my backend API returns the results fine.
Here is a sample URL I'm trying: http://localhost:9000/my/JalapeƱos
A URL requires to be properly encoded. Given your example is a string representing a URI, it's definitely wrong.
You may take a look at NSURLComponents (available for OSX >= 10.9 and iOS >= 7.0) and RFC 3986.
I'm using RESTKit to get data from a rest-api.
This is the URL i set for my request, here's the log just before the request goes off.
2014-04-03 15:51:10.186 xxx[35745:60b] Just sent URL: /api/dspObjGetNewsList?action=coverage&count=30&start=0&open=0&user=xxx&unique=36027&type=all&country=Sweden,global&division=Strategic Industries,Regional Sales and Service,Automotive
Then i log the reponse URL.
- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response {
NSLog(#"xxx: %d, url: %#", [response statusCode], response.URL);
And i get this?
xxx: response code: 200, url: url/api/dspObjGetNewsList?unique=26791&type=all&division=Strategic%20Industries%2CRegional%20Sales%20and%20Service%2CAutomotive&user=xxx&action=coverage&open=0&country=Sweden%2Cglobal&count=30&start=0
Why am i getting a different URL in my response? Does RESTKit modify my url?
Have you configured HTTPClient properly? Use - (id)initWithHTTPClient:(AFHTTPClient *)client method to configure HTTPClient. For instance:
AFHTTPClient *HTTPClient = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:#"www.url.com"]];
Your first log appears to be a raw string of the URL. The second log appears to be the % escaped URL version of that string. This isn't a RestKit thing, it's a URL loading system thing. Certain characters need to be escaped so that they are valid for use in a URL.
For example, your original string has a number of spaces in it. This isn't allowed in a URL and each must be changed to %20.
Why the parameters change order isn't clear - it depends on how you created the string and supplied the parameters to RestKit. But, the order doesn't matter to the processing so you shouldn't need to worry about it.
In my iOS application, i'm posting the request to server using NSURLConnection. The following is the code that I'm using:
NSString* str = #"http://www.myserver.com/post.php?action=rating&grade=A+&name=vasu";
NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
If you see the grade, its equal to "A+"
But its posting only "A" as grade to server. Even though when I'm encoding the string appropriately, why is it not able to post the request properly?
Is it something that has to be handled on server?
The + sign is a reserved character for URL encoding (for 'space'). Although the escaped value for + should be %2B after stringByAddingPercentEscapesUsingEncoding, well... it isn't. What you can do though is to add a simple category into NSString to bypass this problem. You can find a relevant blog post here and some information on URL encoding here