2

I need to invoke a Rest API endpoint from my Lua script. How can I do that? For example, I am able to invoke the endpoint by the below curl command:

curl -X GET \
  -H "X-Parse-Application-Id: ParseAppID" \
  -H "X-Parse-REST-API-Key: RESTAPIKey" \
  https://api.parse.com/1/classes/GameScore

The same I wanted in Lua.

csaar
  • 530
  • 5
  • 22
subrat padhi
  • 151
  • 1
  • 2
  • 11

3 Answers3

1

You have lots of options

All of them are slightly different, but all of them can call your API endpoint.

DarkWiiPlayer
  • 6,871
  • 3
  • 23
  • 38
1

Lua by itself cannot call that endpoint, since the standard networking doesn't support https. You will need to use a 3rd-party library, I suggest Lua-cURL. You will need to download and install it.

AlgoRythm
  • 1,196
  • 10
  • 31
0

Using luasocket:

local http = require('socket.http')
local ltn12 = require('ltn12')

local r = {}
http.request {
    url = 'https://blockchain.info/tobtc?currency=USD&value=1000000',
    headers = {['x-accept'] = 'donates'},
    sink = ltn12.sink.table(r)
}
print(r[1])
x-yuri
  • 16,722
  • 15
  • 114
  • 161
  • Please note that many versions of luasocket (including 3.0-rc1 in Debian Bullseye) [silently downgrades https to http](https://stackoverflow.com/questions/37555226/luasocket-http-requests-always-respond-with-a-redirect-301-or-302). Fixed in 3.0.0 proper according to [MSGH#229](https://github.com/lunarmodules/luasocket/issues/229). – sampi Oct 11 '22 at 12:41