Searching + in a GET service is converted to a white space - ios

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'.

Related

How to decide if an URL should be encoded or not?

I am using URLs downloaded from the Internet, so I don't know their format in advance and they are dynamic.
How to know if I should encode an URL at run time?
// `url` is retrieved from the Internet at run time
// When should I call following line?
let urlString = url!.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLFragmentAllowedCharacterSet())
If it's already encoded, calling stringByAddingPercentEncodingWithAllowedCharacters will make the URL invalid.
Use:NSString *unencodedUrlString = [originalURL stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; to decode the URL. Now compare the decoded version with the original. If they are the same, the url is not encoded, otherwise it is already encoded.

Properly escaping characters for AFNetworking 2.0 URL string

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

RESTKit response.url changed order of parameters

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.

IOS passing backslash as part of url parameter to Json Web Service

I'm calling a Json Web Service wich expects two parameters. username and password.
The username I'm trying to pass includes a backslash (\) as it is a domain account eg. companyname\jamesd.
Here is my code.
NSString *sName = #"companyname\\jamesd";
NSString *URL = [NSString stringWithFormat:#"http://checkin.companyname.com:2002/Checkin.svc/checklogin?username=%#&password=pass",sName];
Problem is the backslash is somehow removed when calling the web service -
I use NSLog(...) to output what the request looks like -
<NSURLRequest http://checkin.companyname.com:2002/Checkin.svc/checklogin?username=companynamejamesd&password=pass>
I'm trying to find out how I can force include the backslash in the http request for the username.
Like Mike Weller stated in the comment you have to url-encode the arguments. You should use the stringByAddingPercentEscapesUsingEncoding: method of NSString after you create the url string something like:
NSString *URL = [NSString stringWithFormat:#"http://checkin.companyname.com:2002/Checkin.svc/checklogin?username=%#&password=pass",sName];
URL = [URL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
You can change the encoding to as you want, check the iOS encoding on Apple Docs
Also make sure that you are adding the backslash when you're creating the url string.

iOS - NSURLConnection - Proper encoding of characters in request

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

Resources