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.
Related
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'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.
I have to pass JSON dictionary as POST data to a Webservice. One key involves an Amazon S3 URL string.
The sample request json which works has the URL as....
https:\/\/myappbucket.s3.amazonaws.com\/2014230407_102323.jpg?response-content-type=image\/png&Signature=123456%3D&Expires=139756222548&AWSAccessKeyId=ABCDEF
Notice the backslashes just before the forwardslashes? I have never seen a URL like that, but thats how I'm supposed to pass it.
I tried
stringByAddingPercentEscapesUsingEncoding
and
stringByReplacingPercentEscapesUsingEncoding
while using NSASCIIStringEncoding and NSUTF8StringEncoding
Can anyone make sense of this?
if we try to convert url into legal url trough stringByAddingPercentEscapesUsingEncoding than it adds all percent escapes necessary to convert the receiver into a legal URL string.Uses the given encoding to determine the correct percent escapes.
if we use stringByReplacingPercentEscapesUsingEncoding than it replaces all percent escapes with the matching characters as determined by the given encoding.
mostly to get valid url, we can use NSUTF8StringEncoding to remove backslashes just before the forwardslashes in url.
Generally, you should use a JSON serializer library (e.g. NSJSONSerialization) in order to obtain a JSON from a JSON representation and not try to create the JSON yourself.
A JSON representation is a NSDictionary or NSArray object containing other objects which recursively represent your JSON. Your URL will be represented as a NSString.
What you need to do is simply have a valid URL as a NSString, properly encoded according RFC 3968 and assign it the JSON representation, e.g.:
NSURL* url = ...;
NSDictionary* jsonObject = #{#"url": [url path]};
Now, you can serialize the JSON representation to a JSON:
NSError* error;
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:jsonObejct
options:0
error:&error];
That's it, and you don't bother how the JSON encoded string looks like (encapsulated in the NSData object as a UTF-8 character sequence).
Purposefully, when you POST this JSON, you SHOULD specify a corresponding request header:
ContentType: application/json
which lets you just use the JSON data as body data as is:
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
request.HTTPBody = jsonData;
Side note: [url path] returns a URL as a string according RFC 1808 which is obsoleted by RFC 3968 since January 2005 already. Today, there are newer APIs since iOS 7.0, see NSURLComponents.
When parsing strings into NSURL objects, NSURL treats a string using a single forward slash differently to a string with a double forward slash after the scheme.
Why is this the case?
Here's some examples:
NSURL *url = [NSURL URLWithString:#"app-id://path1/path2"];
NSLog(#"%#",url.scheme); // "app-id"
NSLog(#"%#",url.path); // "/path2"
NSLog(#"%#",url.host); // "path1"
NSURL *url = [NSURL URLWithString:#"app-id:/path1/path2"];
NSLog(#"%#",url.scheme); // "app-id"
NSLog(#"%#",url.path); // "/path1/path2"
NSLog(#"%#",url.host); // nil
The two strings are treated differently because the first one with // complies with RFC 1808 and the second string does not.
The first string is a standard internet URL, and the second string omits the 'Authority Component' (see RFC 2396 Section 3.2) and begins the path component immediately, therefore not conforming to RFC 1808.
RFC1808
RFC 1808 defines the "Relative Uniform Resource Locators" which are the most familiar URIs these days and use the // format:
<scheme>://<net_loc>/<path>;<params>?<query>#<fragment>
The //<net_loc> is a required component of 1808 and RFC 2396 states that the component after the // is called the Authority Component (see seciton 3.2) which acts as the top element, or namespace for the path. The path begins at the next forward slash.
The authority component is preceded by a double slash "//" and is
terminated by the next slash "/", question-mark "?", or by the end of
the URI.
The single forward slash
Using a single forward slash ommits the Authority Component and starts the path component immediately.
NSURL
NSURL exposes the Authority Component of a URL as the host parameter and mentions that it's only available as part of URLs conforming to 1808:
[host] Return Value
The host of the URL. If the receiver does not conform to RFC 1808, returns nil.
This is why NSURL treats them differently, why the host component is nil in the second case and why the path contains everything after the colon.
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