How to pass "&" as string in a URL? [duplicate] - ios

This question already has answers here:
iOS: Issue with ampersand in the URL string
(6 answers)
Closed 9 years ago.
In my app I am passing a string to service which sets some data to remote database. Everything works perfect as far as I don't have "&" in my string.
I am using this to pass the parameters:
NSString *urlString = [[NSString
stringWithFormat:#"http://someservice.com/some/some_setstatus.php?status=active&name=%#",
merchant] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
My guess is when it's checking the name with the name saved in the database, it's not able to find one because of '&'.

You are unable to pass "&", as it will be treated as a delimiter for URL parameters. "&" is a reserved character and you can consider encoding "&" as a combination of allowed characters. Take a look at RFC 3986 and the answer to this question.

You need to escape the & character for url (more info: http://www.ietf.org/rfc/rfc3986.txt)
Have a look at https://github.com/tagyro/GTM/blob/master/Foundation/GTMNSString%2BURLArguments.h
This is a clone of Google Toolbox for Mac from their SVN.

I believe you need to urlEncode to the string which will properly encode any special characters. See http://www.w3schools.com/tags/ref_urlencode.asp for more information.
Regards,
Steve

Related

Datausingencoding that doesn't replace plus signs

I'm looking for a datausingencoding parameter that doesn't swallow up plus signs. I was using NSASCIIENCODING but since I'm trying to send a uiimage to the server and the base64 string had plus signs in them, it seems like that form of encoding takes out the plus sign sending a modified encoded string to the server thereby not allowing the image to be decoded server side. I'm looking for something that won't alter the base64 string.
Nevermind guys, here is the solution I found on stackoverflow
thanks, now I figured it out. It seems I needed to run my string through the stringByAddingPercentEscapesUsingEncoding: first, then I needed to run it through replaceOccurrencesOfString:#"+" withString:#"%2B" and several more of those replaces for different characters, because stringByAddingPercentEscapesUsingEncoding: doesn't escape them all

What the meaning of "?" in the PHP URL [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What is the “?” symbol in URL used for in php?
In the websites based on PHP usually has "?" in URL,
i.e. http://host/wordpress/?p=276
What is the meaning of "?" in the URL?
The "?" is not related to PHP only. This is the symbol which starts the begining of query. Format is:
www.serverhost.com/script.php?parameter1=A&parameter2=B
When browser requests server with this query the server parses it and passes parameters to the script, for example in PHP you will receive those parameters in $_GET array, i.e:
$_GET['parameter1'] will contain 'A', and $_GET['parameter2'] will contain 'B'.
It separates the URL path from the query string.
http://en.wikipedia.org/wiki/Uniform_Resource_Locator
You might want to have a look at the query string
It's using GET to store a variable called p with a value of 277.
In PHP you can read that variable by running
<?
echo $_GET['p'];
?>
On this page that would say 277.
In this case, it's used to mean this is post number 277. The code will check this is a number, then look up the post with id 277 in the database.
seperation of http address and parameters passed to that address
? indicates the beginning of the query part of the URI. Please refer RFC3986
When the forms (like login or register or any other kind of forms) are sending values )onclick of submit button) if that form is sent by 'GET' method then the values sent are visible in URL with appending '?' at the end and values next to that

Can we use & in url?

Can we use "&" in a url ? or should "and" be used?
Yes, you can use it plain in your URL path like this:
http://example.com/Alice&Bob
Only if you want to use it in the query you need to encode it with %26:
http://example.com/?arg=Alice%26Bob
Otherwise it would be interpreted as argument separator when interpreted as application/x-www-form-urlencoded.
See RFC 3986 for more details.
An URL is generally in the form
scheme://host/some/path/to/file?query1=value&query2=value
So it is not advisable to use it in an URL unless you want to use it for parameters. Otherwise you should percent escape it using %26, e.g.
http://www.example.com/hello%26world
This results in the path being submitted as hello&world. There are other characters which must be escaped when used out of context in an URL. See here for a list.
Unless you're appending variables to the query string, encode it.
encode '&' with & (this answer is based on your use of tags)
If you are asking what to use "&" or "and" when registering the name of your URL, I would use "and".
EDIT: As mentioned in comments "& is an HTML character entity and not a URI character entity. By putting that into a URI you still have the ampersand character and additional extraneous characters." I started answering before fully understanding your question.

Properly url encode space character

I use HttpUtility.UrlEncode to encode any value that is used in a route.
I already solved an issue with encoding forward slashes. The new problem I have now is with spaces. A space is encoded as + .
This works on the VS integrated Webserver, but I have an issue with it in IIS7 on Windows Server 2008.
If I have the URL http://localhost/Home/About/asdas+sdasd
I get the error 404.11 - Request contains double escape sequence.
I know I can just replace the space by "%20", but I dont want to care about propper encoding myself. Is there any ready to use UrlEncoder for MVC out there?
' ' encoded to %20 use HttpUtility.UrlPathEncode.
Any URL Encoding is most often designed to work on the path component of the url, the reason because different schemes have different characters in the safe list. Look for your libraries urlencoder and just use it in the path and above portion of the url.
#HttpUtility.UrlPathEncode(path)
UrlPathEncode just encodes the path of the Url, rather than encoding the whole Url.

question about URLs

is it possible to have a url that has a param with an & embedded in the value (eg &FirmName=Test&Firm) and not have the browser treat that & in the value as the beginning of another param???
Yes, just encode the "&" using URL encoding ("%26")
Most web programming frameworks have functions that take a string and URL-encode it
You need to urlencode it - & is %26:
&FirmName=Test%26Firm

Resources