how to send headers on Lua script?

Viewed 39

Hi I want to send headers in lua.

I write code like this

local response = http.get(url,{headers:{'x-api-key':MY API KEY}})

but I got expected near '{' message.

how to I send header on lua script? plz help me..

1 Answers

but I got expected near '{' message.

This is a clear syntax error. You should be able to fix these yourself after googling for them, consulting the reference manual. Your editor highlighting syntax errors is also useful; linters like luacheck provide this.

The reason is that Lua does not use : (like JS or JSON) but = for delimiting key-value pairs; key-value pairs are written as [key] = value, with the special exception that keys which are identifiers / "names" (could be used as variable names) can conveniently be written as name = value. This allows us to write headers = {...}. ['x-api-key'] = ... can't be shortened this way because hyphens aren't allowed in variable names in Lua.

Replace your code with the following line:

local response = http.get(url,{headers = {['x-api-key'] = MY API KEY}})

to fix the syntax error.

Related