Skip to content

Instantly share code, notes, and snippets.

@tmlbl
Last active August 29, 2015 14:16
Show Gist options
  • Save tmlbl/80cee11b1309deb03749 to your computer and use it in GitHub Desktop.
Save tmlbl/80cee11b1309deb03749 to your computer and use it in GitHub Desktop.
Basic query string encoder in Julia
using HttpCommon
using Dates
# Creating a query string that will encode unix datetimes
# given Julia DateTimes. Methods to handle more types could
# easily be added.
function qstring(kv::Tuple...)
encodeval(v::String) = encodeURI(v)
encodeval(dt::DateTime) = string(int64(datetime2unix(dt)))
encodepair(p::Tuple) = string(encodeval(p[1]), "=", encodeval(p[2]), "&")
q = string("?", join(map(encodepair, kv)))
q[1:length(q) - 1]
end
# Let's test it!
@assert qstring(("a", "b"), ("c", "d")) == "?a=b&c=d"
@assert qstring(("date", unix2datetime(1424888079))) == "?date=1424888079"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment