Howdy! I'm trying to use ajaxGet() from Karax and I need to set the "Accepts" header to json, but I can't figure out how to make the openArray[(cstring,cstring)] that the ajaxGet() call expects for the headers.
I've tried all sorts of different combinations, including making a tuple, a sequence of tuples. I suspect there's a way to make a literal, but I don't know and can't see anything in the docs that helps me make an array of pairs of things.
Would appreciate any pointers for my newbie question. Brian
For array/seq (which known as openArray when used as argument type), we can define the type easily by explicitly type the first element type like
let int8Arr = [byte 1, 2, 3, 4, 5] // array[0..4, byte]
var int16seq = @[1'i16, 2, 3, 4, 5] // seq[int16]
But not so with tuple
let cstrarr = [(cstring"one", 1), ("two", 2), ("three", 3)] # will yield compile error
So we have no other choice other type manually to convert it, or resort to proc/template to convert it
proc headers[V](hds: varargs[(string, V)]): (cstring, V) =
result = @[] # newer Nim doesn't have to initialize it anymore
# notice the we add the "tuple", so add space after proc `add`
# ofc we can also write it like result.add((cstring k, v))
# when in doubt which to use, use parenthesis
for k, v in hds: result.add (cstring k, v)
let ts = @[("a0".cstring,"a1".cstring),("b0".cstring,"b1".cstring),("c0".cstring,"c1".cstring)] test(ts)
@[("Accepts".cstring,"application/json".cstring)] was the correct incantation.
Thanks again for the help.
Note that:
@[("Accepts".cstring, "application/json".cstring)]
is the same as:
{"Accepts".cstring: "application/json".cstring}