V
V
Vasily Melnikov2018-10-09 20:27:34
Redmine
Vasily Melnikov, 2018-10-09 20:27:34

How to write lua request to REST API?

I decided to write a lua script for requests to the redmine server and did not figure out how the functions of the socket and socket.http packages work. returns nil
c = socket.connect("local.redmine", 80)

strUrl = " /issues.xml"
strUrl = strUrl.."?key="..apiAccessKey
strUrl = strUrl.."&assigned_to_id=me"
strUrl = strUrl.."&status_id=2"
strUrl = strUrl.."&limit=100"
strUrl = strUrl.."\r\n\r\n"
res, c, cache = http.request("local.redmine", "GET", strUrl)

in res reads the html code of the main page of the site
, I tried some such variations
local headers = {}

headers["Content-Type"] = "application/json"
headers["X-Parse-Application-Id"] = "LuaTest"
headers["X-Redmine-API-Key"] = apiAccessKey

local body = ""
local params = {}
params.headers = headers
params.body = body
res, c, cache = http.request("local.redmine", "GET", networkListener, params)

In C++, it looked like this, and it worked quite well for itself
SOCKET Socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
  struct hostent *host;
  host = gethostbyname("local.redmine");
  SOCKADDR_IN SockAddr;
  SockAddr.sin_port=htons(80);
  SockAddr.sin_family=AF_INET;
  SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);
  connect(Socket,(SOCKADDR*)(&SockAddr),sizeof(SockAddr));

  string strUrl = "GET /issues.xml";
  strUrl += "?key=" + apiAccessKey;
  strUrl += "&assigned_to_id=me";
  strUrl += "&status_id=2";
  strUrl += "&limit=100";
  strUrl += "\r\n\r\n";
  
  send(Socket, strUrl.c_str(), strUrl.length(), 0);
  char buffer[10001];
  int nDataLength;
  while ((nDataLength = recv(Socket,buffer, 10000,0)) > 0){
    buffer[nDataLength] = 0;
    m_strOut += buffer;
    strXmlAnswer += buffer;
  }
  closesocket(Socket);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
kiriharu, 2019-12-26
@kiriharu

Socket seems to have http, so you can do it like this:

local http = require("socket.http")

local body, code, headers, status = http.request("http://example.com/lua.php")

if body then
   -- если есть ответ - пишем его
    print(body)
else 
    -- если нет - скорее всего произошла какая-то ошибка. Выводим код ошибки.
    print(code)
end

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question