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
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 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
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'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.
I am trying to get an html response from a server(aspx).
NSString *urlstr = [[NSString alloc] initWithFormat:#"http://www.someSiteThatRedirectsWithGetParameters.com?parameter1=1¶meter2=2"];
NSURL *url = [ NSURL URLWithString:urlstr];
NSMutableURLRequest *urlRequest = [ NSURLRequest requestWithURL: url];
NSData data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];
NSURL *lastURL=[response URL];
NSLog(#"Last url %#", lastURL);
I am getting lastURL as www.someSiteThatRedirectsWithGetParameters.com and therefore not getting the html page for the original requested url.
Why is this so and how can I solve this?
EDIT
This is all what I have tried so far : Get the last redirected url in iOS 5?
but I am still not able to achieve what I am trying to i.e : get the html page source of the original GET request(going through redirections) having parameters in the url ?
I am getting lastURL as www.someSiteThatRedirectsWithGetParameters.com
and therefore not getting the html page for the original requested
url.
If your request is being redirected, it's because the server to which you're connecting is telling you that there is no page at the URL to which you first made the request, and you should use the redirected URL instead. That's the whole point of a redirect response.
If the response that you get is a HTTP 303, the new request will properly be changed from POST to GET. This is sometimes done to avoid cases where refreshing the page would resubmit the original request with undesirable consequences. In this case, you POST your request to some URL, and the server accepts the request but responds by redirecting the client to a different URL which ultimately displays the server's response.
Why is this so and how can I solve this?
I've explained the 'why' above. The "solution" is to accept the response from the server and follow the redirect to the URL that contains the server's response.
I would try the approach of sending two requests. Send the first request, then the redirectURL will be the response. Then make a new URL of the response, sending a request to that url with the GET-parameters. I haven't tried it, but I suppose it could work.