mirror of
https://github.com/luanti-org/luanti.git
synced 2025-06-27 16:36:03 +00:00
Add PUT and DELETE request + specific method value to HTTP API (#9909)
This commit is contained in:
parent
f34abaedd2
commit
715a123a33
8 changed files with 98 additions and 44 deletions
|
@ -49,17 +49,40 @@ void ModApiHttp::read_http_fetch_request(lua_State *L, HTTPFetchRequest &req)
|
|||
req.multipart = getboolfield_default(L, 1, "multipart", false);
|
||||
req.timeout = getintfield_default(L, 1, "timeout", 3) * 1000;
|
||||
|
||||
// post_data: if table, post form data, otherwise raw data
|
||||
lua_getfield(L, 1, "method");
|
||||
if (lua_isstring(L, -1)) {
|
||||
std::string mth = getstringfield_default(L, 1, "method", "");
|
||||
if (mth == "GET")
|
||||
req.method = HTTP_GET;
|
||||
else if (mth == "POST")
|
||||
req.method = HTTP_POST;
|
||||
else if (mth == "PUT")
|
||||
req.method = HTTP_PUT;
|
||||
else if (mth == "DELETE")
|
||||
req.method = HTTP_DELETE;
|
||||
}
|
||||
lua_pop(L, 1);
|
||||
|
||||
// post_data: if table, post form data, otherwise raw data DEPRECATED use data and method instead
|
||||
lua_getfield(L, 1, "post_data");
|
||||
if (lua_isnil(L, 2)) {
|
||||
lua_pop(L, 1);
|
||||
lua_getfield(L, 1, "data");
|
||||
}
|
||||
else {
|
||||
req.method = HTTP_POST;
|
||||
}
|
||||
|
||||
if (lua_istable(L, 2)) {
|
||||
lua_pushnil(L);
|
||||
while (lua_next(L, 2) != 0) {
|
||||
req.post_fields[readParam<std::string>(L, -2)] = readParam<std::string>(L, -1);
|
||||
req.fields[readParam<std::string>(L, -2)] = readParam<std::string>(L, -1);
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
} else if (lua_isstring(L, 2)) {
|
||||
req.post_data = readParam<std::string>(L, 2);
|
||||
req.raw_data = readParam<std::string>(L, 2);
|
||||
}
|
||||
|
||||
lua_pop(L, 1);
|
||||
|
||||
lua_getfield(L, 1, "extra_headers");
|
||||
|
|
|
@ -32,10 +32,10 @@ private:
|
|||
static void read_http_fetch_request(lua_State *L, HTTPFetchRequest &req);
|
||||
static void push_http_fetch_result(lua_State *L, HTTPFetchResult &res, bool completed = true);
|
||||
|
||||
// http_fetch_sync({url=, timeout=, post_data=})
|
||||
// http_fetch_sync({url=, timeout=, data=})
|
||||
static int l_http_fetch_sync(lua_State *L);
|
||||
|
||||
// http_fetch_async({url=, timeout=, post_data=})
|
||||
// http_fetch_async({url=, timeout=, data=})
|
||||
static int l_http_fetch_async(lua_State *L);
|
||||
|
||||
// http_fetch_async_get(handle)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue