Invoke Rest api end point in LUA

£可爱£侵袭症+ 提交于 2021-01-27 22:16:05

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!