From bb7f9c6270fd251137e93455e0f7a3f25f12a7e0 Mon Sep 17 00:00:00 2001 From: Paul Kulchenko Date: Sat, 24 Jul 2021 00:22:26 -0700 Subject: [PATCH] Update redbean Fetch to accept table with parameters (#97). --- tool/net/redbean.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/tool/net/redbean.c b/tool/net/redbean.c index 9446f68f6..986644407 100644 --- a/tool/net/redbean.c +++ b/tool/net/redbean.c @@ -3702,15 +3702,21 @@ static int LuaFetch(lua_State *L) { .ai_flags = AI_NUMERICSERV}; /* - * Get args. + * Get args: url [, body | {method = "PUT", body = "..."}] */ urlarg = luaL_checklstring(L, 1, &urlarglen); - if (lua_isnoneornil(L, 2)) { + if (lua_istable(L, 2)) { + lua_settop(L, 2); // discard any extra arguments + lua_getfield(L, 2, "body"); + body = luaL_optstring(L, -1, ""); + lua_getfield(L, 2, "method"); + method = GetHttpMethod(luaL_optstring(L, -1, ""), lua_rawlen(L, -1)); + lua_settop(L, 2); // drop all added elements to keep the stack balanced + } else if (lua_isnoneornil(L, 2)) { body = ""; method = kHttpGet; } else { - size_t bodylen; - body = luaL_checklstring(L, 2, &bodylen); + body = luaL_checkstring(L, 2); method = kHttpPost; }