Why do I need to Url Encode my http POST requests?

Why do I need to Url Encode my http POST requests?

Good question, but before getting to this, let’s have a bit of background on what a http POST request is.

A http POST is a request method used by a web server to request that the server should accept data passed in the POST .

Ok, so what is the server expecting to receive?

The server will expect so receive the data as a key-value pair.
An example might be animal=cat the key – animal, relates to the value (data) cat. Think of the key as a label you attach to the data (value) so that it can be identified when it gets to the server.

Hmm, so what does the POST animal=cat actually look like and how should it be constructed?

As an example if I were to POST animal=cat to ‘http://2108.co.uk/code/lc/mypost.lc’ , the constructed url would look like this: ‘http://2108.co.uk/code/lc/mypost.lc?animal=cat’ the ‘?’ tells the server that the query string it’s receiving will contain at least one key-value pair.

If you need to send more than one key-value pair then you need to insert the character ‘&’, so to send animal=cat and color=black the constructed url would look like this ‘http://2108.co.uk/code/lc/mypost.lc?animal=cat&color=black’.

OK so far so good, but what would happen if you were to send for example ‘color=black&white’ ? …the server will get confused! how should it know that the ‘&’ in ‘color=black&white’ is not joining 2 key-value pairs, it doesn’t and will assume that it is, and your key-value pairs will be mixed and re-ordered, result .. a real mess!

Url Encode to the rescue!

What does Url Encoding actually do?

It converts the (query)string into a valid ASCII format. Why? because URLs are sent over the Internet using the ASCII character-set.

There are two main groups of characters that need to be evaluated.

The unreserved characters are not encoded.

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
a b c d e f g h i j k l m n o p q r s t u v w x y z
0 1 2 3 4 5 6 7 8 9 – _ . ~

The reserved characters have to be encoded ONLY IF THEY ARE PART OF THE KEY-VALUE STRING otherwise they can be treated the same as the unreserved group.

Reserved characters are:

! * ‘ ( ) ; : @ & = + $ , / ? % # [ ]

The conversion process outputs the converted characters as a triplet.
A “%” followed by two hexadecimal digits.

If we take the previous example, ‘http://2108.co.uk/code/lc/mypost.lc?animal=cat&color=black&white’ then the encoded string becomes ‘http://2108.co.uk/code/lc/mypost.lc?animal=cat&color=black%26white’ the ‘&’ in ‘black&white’ has been converted (encoded) to ‘%26’

In LiveCode the script to do this would be:

on mouseUp
//the '&' in the string will cause problems so url encode it
put urlEncode("black&white") into tencodedString
put "animal=" & cat & "&color=" & tencodedString into tmyData
post tmyData to url "http://2108.co.uk/code/lc/mypost.lc?"
end mouseUp

Next time I will explain with LiveCode examples how to work with this POSTED information.