问题
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.
回答1:
You have lots of options
- LuaSocket
- Lua-HTTP
- Lua-cURL
- (probably more)
All of them are slightly different, but all of them can call your API endpoint.
回答2:
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.
回答3:
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])
来源:https://stackoverflow.com/questions/58849867/invoke-rest-api-end-point-in-lua