Utility for handling http querystrings and other http related stuff such as:
- Querystring parsing
- Querystring to Json
- Json to querystring
- Percent encoding and decoding
- Queristring nested bracket notation utils
- Json value generic parsing from string
Just use the following command:
$ boss install github.com/Joao-Peterson/httputils
If asked for permission, you can login with your gitlab credentials. (must be done in powershell or cmd with administrator mode)
$ boss login
> Url to login (ex: github.com): github.com
> Use SSH(y or n):
> n
> Username: your_username
> Password: your_password
Note: ssh isn't supported, hence the 'n' for not using ssh. See this issue with boss: HashLoad/boss#52.
Start by including including querystringU.pas
.
A querystring is a &
delimited string with key/value pairs that is percent/url encoded and is usually present in the http URI:
http://site.com?key=value&key2=value2&encodedvalue=%20%3A%2F%
Or inside the body of a Content-Type: x-www-form-urlencoded
http request:
http://site.com
Content-Type: x-www-form-urlencoded
Body:
key=value&key2=value2&encodedvalue=+%3A%2F%
With the queristringT
class you can easly parse this kind of data to multiple formats, including JSON!.
var qs: querystringT := querystringT.Create();
qs.parseQuerystring('key=value&key2=value2&encodedvalue=+%3A%2F%');
var qs: querystringT := querystringT.Create();
qs.parseJson('{"key":"value","key2":"value2","encodedvalue":" :/"', true);
Or from a JsonObject:
var json := TJSONObject.ParseJSONValue('{"key":"value","key2":"value2","encodedvalue":" :/"') as TJSONObject;
var qs: querystringT := querystringT.Create();
qs.parseJson(json, true);
The last argument refeers to as how the json null
should be interpeted.
If true
then value: "null"
If false
then value: ""
var qs: string := qs.toQuerystring(true);
Where the true
sets the output to be percent/url encoded.
Can be used to access key/values pair.
var qs: TStringList := qs.toQuerystringList(true);
WriteLN(qs.Keys[0]);
WriteLN(qs.Values['key1']);
WriteLN(qs.ValuesByIndex['0']);
Where the true
sets the output to be percent/url encoded.
Notes:
- Nested values in querystring form are of the form
value[child][grandchild]=42
. - Nested values are converted to nested json objects.
{"value":{"child":{"grandchild": 42}}}
. - Nested values with numbers are converted to arrays.
value[1][grandchild]=42
->{"value":["grandchild": 42]}
. - Sometimes the conversion from querystring to json to querystring again can be not equal, that's because compromisses are made in order to achieved a more natural json, no value will be lost, but if a name contains illegal characters it may be parsed as a separator, so the json will be generated differently and consequentially the new querystring.
var json: string := qs.toJson(true, true, true, true);
Where the 4 arguments are:
parseBool: Boolean = true // define if 'true' and 'false' should be parsed from string to Boolean if true
parseNull: Boolean = true // define if 'null' should be parsed from '' to 'null' if true
parseFloats: Boolean = true // define if a float should be parsed from a string to Extended if true
parseIntegers: Boolean = true // define if a integer should be parsed from a string to Integer if true
var json: TJSONObject := qs.toJsonObject(true, true, true, true);
Where the 4 arguments are:
parseBool: Boolean = true // define if 'true' and 'false' should be parsed from string to Boolean if true
parseNull: Boolean = true // define if 'null' should be parsed from '' to 'null' if true
parseFloats: Boolean = true // define if a float should be parsed from a string to Extended if true
parseIntegers: Boolean = true // define if a integer should be parsed from a string to Integer if true
Start by including including httpUtilsU.pas
.
This utils are for handling miscellaneous http related tasks, feel free to read about them, they might be useful for your use case.
All methods are class
ones.
var encoded: string := httpUtilsT.percentEncode(input, true);
The second boolean is for switching the URI
/x-www-form-urlencoded
type, the difference is:
uri: ' ' = '%20'
urlencoded: ' ' = '+'
Decoded is of the same form:
var decoded: string := httpUtilsT.percentDecode(encoded, true);
substitute points on a string for '[' and ']'. Ex: 'a.b.c' -> 'a[b][c]'
class function pointsToBrackets(input: string): string;
substitute brackets on a string for '.'. Ex: 'a[b][c]' -> 'a.b.c'
class function bracketsToPoints(input: string): string;
split a string by brackets. Ex: 'a[b][c]' -> '(a, b, c)'
class function bracketsSplit(input: string): TArray<String>;
Given a string that may contain a json standard value, such as true
, null
or -23.45E+5
, this function can parse the value from a string to the correct type using a record
with case
:
var parsed: jsonValueT := httpUtilsT.parseJsonValue(valueString, true, true, true, true);
case parsed.jsonType of
jsonNull: // indicates a null value;
jsonBoolean: var num: boolean := parsed.booleanValue; // access boolean value
jsonInteger: var num: integer := parsed.integerValue; // access integer value
jsonFloat: var num: extended := parsed.floatValue; // access extended value
jsonString: var num: string := parsed.stringValue; // access string value
end;
Tests are done by the testsU.pas file using DunitX, or rather, a fork i made that uses boss to be installed.
Just use try-exception
blocks.
- URIBuilder()