I'm busy with web service and an iOS app. So far I haven't had that many issues. However, there is a web service that needs the final URL request to be in a certain format. Something along the lines of:
...?IncludedUserIds[]=1357213,286476&..
These parameters are constructed from an NSDictionary and NSString. Now when I add the comma - the end URL that makes the request ends up like this:
..?IncludedUserIds=%5B%5D1357213%2C286476&...
It seems that AFNetworking 2.0 has converted the square brackets into =%5B%5D and the comma into: %2C
Obviously, the web service has no idea what this means and fails.
Is there a way to keep the final Url as I need it to be? Why do these conversions happen and where can I learn more about this sort of thing?
AFNetworking keep the final url encoded, that is the best way, since you could have special characters in you query string that could break all. Instead, you should decode the url server side, for example in PHP you have methods, like urldecode or rawurldecode (eg: 'foo%20bar%40baz' ->'foo bar#baz'), or in ASP it should be kept automatically.
Hope it helps
Related
I am sending an XML file using chain in Grails like so:
chain(controller:"somecontroller", action:"someaction", params:[wib:wib.encodeAsURL()])
Where wib is an XML string. When I use a smaller XML string this works fine.
It is worth noting that the XML string is about 50kb in size.
Any ideas why this doesn't work?
It doesn't even get to the next action.
I personally thing it is to do with the size of the URL in my browser.
Since the chain method issues an HTTP redirect sending 50kb worth of information in the parameters (query parameters) isn't going to work. I seem to recall the maximum limit is somewhere around 4,000 characters for the entire query string.
That said, you may have better luck passing that data in a model, but still that's going to be putting all that data into session (flash) scope.
You can read more about the options for chain in the Grails documentation.
I am using AFNetworking to perform login and the url format is like this:
http://xxx/mobile?function=login&req={username:xxx,password:xxx}
So firstly I created the parameters using a NSDictionary within a NSDictionary, like below:
#{#"function" : #"login",
#"req" : #{#"username" : #"xxx", #"password" : #"xxx"}}
But the query comes out is wrong
function=login&req[password]=xxx&req[username]=xxx
After this, I used JSONKit to package the parameter
NSDictionary *userInfo = #{#"userName" : [username URLEncodedString],
#"password" : [password URLEncodedString]};
NSDictionary *parameters = #{#"function" : #"login",
#"req" : [userInfo JSONString]};
And the results seem alike but "{}" encoded
function=login&req=%7B%22userName%22%3A%22xxx%22%2C%22password%22%3A%22xxx%22%7D
Where did I goes wrong? How can I correct it?
Many Thanks!
There are a couple of issues here.
You seemed to be concerned about the percent escapes in the portion of the URL that looks like:
function=login&req=%7B%22userName%22%3A%22xxx%22%2C%22password%22%3A%22xxx%22%7D
You shouldn't be concerned about that, because when the server receives it, it will automatically convert that to:
function=login&req={"userName":"xxx","password":"xxx"}
You report that an associate is telling you that the percent escapes are not needed. Technically, he might be right (you might get away without percent-escaping in this case, as long as the username and password do not have certain particular reserved characters, notably +, & or #, which can cause problems in URLs), but per the RFC's regarding URLs, all of those characters really should be percent-escaped. And the the server will automatically un-percent-escape the parameter, so you shouldn't worry about the fact that the request was percent-escaped. And, in fact, if you want this interface to be robust, both the iOS and Android clients really should be percent-escaping values like this that you add to the URL.
What would appear to be problematic, though, is that the above example includes double quotes, whereas you suggested that the server is expecting something like:
{username:xxx,password:xxx}
But that's not valid JSON. Valid JSON would be:
{"userName":"xxx","password":"xxx"}
So, are you really confident that your web service wants/needs invalid JSON? If you absolutely need invalid JSON (and I'd be shocked if that's where you want to go on this), you'd have to build it manually with stringWithFormat, as any JSON class worth its weight in salt would not be able to generate the JSON without the quotes.
I'd discourage you from sending userid and password information in the URL. That represents a security risk, where this information can easily be compromised. You really should be putting these authentication details in the body of a POST request, not in the URL of a GET request. (And, obviously, you'd want to use HTTPS, not HTTP.)
I also question, with all due respect, the approach of mixing x-www-form-urlencoded style parameters (for function and req), but then using JSON for the userName and password. Generally you'd either use x-www-form-urlencoded style requests, or JSON requests, but it's very curious to combine the two together. You can, but it's just a little strange.
So, bottom line, while you can build the URL you describe (doing it manually with stringWithFormat), I'd really encourage you to redesign your web service to (a) receive POST requests with authentication details in the body of the request; and (b) use either JSON or x-www-form-urlencoded, but you probably don't want to mix-and-match.
I have developed a ASP.NET MVC application. I have a conroller with the name EmployeeController and it got a method called GetEmployeeByName. GetEmployeeByName() takes a name of type string as parameter.
So When I send a request like this, i get the data back :
someDomain:9999/Employee/GetEmployeeByName/Roger Federer
But if the name contains an '&' (you & me), I get a '400 Bad Request' as response from server.
someDomain:9999/Employee/GetEmployeeByName/you%20&%20me
Even if i encode it dont get a reposne back
someDomain:9999/Employee/GetEmployeeByName/you%20&%20me
What is the right way to encode such (data with special character) data?
What is the right way to encode such (data with special character) data?
The right way is to use a query string parameter and not be putting those things as part of the uri portion. Read the following blog post from Scott Hansleman. I will only quote hos conclusion:
After ALL this effort to get crazy stuff in the Request Path, it's
worth mentioning that simply keeping the values as a part of the Query
String (remember WAY back at the beginning of this post?) is easier,
cleaner, more flexible, and more secure.
As you can see in the blog post there are some hacky ways to make it work and circumvent IIS handling but it simply is not something that I would recommend you venturing into. Just put this name in the query string.
I find this behavior a bit strange, I'm using 'net\http' to do some restful communication to an internal API. For this I need to send a multipart/form-data request to our server. In my code I have this:
request["Content-Type"] = "multipart/form-data, boundary=AbCdE1"
The request created then looks like this:
"multipart/form-data, boundary=abcde1"
The problem is the body itself is using AbCdE1 for its boundaries and it fails. Obviously I could use just lowercase letters, but for a more reliably unique boundary, having capitals is helpful. I have seen comments that rails makes headers lowercase, is there a good reason why it does this without my intervention?
The correct delimiter for the Content-Type and the boundary is a semicolon... Have you tried to use a semicolon instead of the comma, to see if Rails still translate it to lowercase?
BTW check http://httparty.rubyforge.org/ for consuming RESTful webservices, the result is more elegant than writing net/http code.
If an extra character (like a period, comma or a bracket or even alphabets) gets accidentally added to URL on the stackoverflow.com domain, a 404 error page is not thrown. Instead, URLs self correct themselves & the user is led to the relevant webpage.
For instance, the extra 4 letters I added to the end of a valid SO URL to demonstrate this would be automatically removed when you access the below URL -
https://stackoverflow.com/questions/194812/list-of-freely-available-programming-booksasdf
I guess this has something to do with ASP.NET MVC Routing. How is this feature implemented?
Well, this is quite simple to explain I guess, even without knowing the code behind it:
The text is just candy for search engines and people reading the URL:
This URL will work as well, with the complete text removed!
The only part really important is the question ID that's also embedded in the "path".
This is because EVERYTHING after http://stackoverflow.com/questions/194812 is ignored. It is just there to make the link, if posted somewhere, if more speaking.
Internally the URL is mapped to a handler, e.g., by a rewrite, that transforms into something like: http://stackoverflow.com/questions.php?id=194812 (just an example, don't know the correct internal URL)
This also makes the URL search engine friendly, besides being more readable to humans.