Skip to main content

HttpClient.WebRequest(string url, HttpMethod method, object table, HeaderCollection extraHeaders = Nil)

Sends a new Wen Request with Json content. A URL, HTTP Method and an object are required. The Object will be serialized to Json string internally.

Parameters:

NameTypeDescription
urlstringWeb request URL.
methodHttpMethodRequest Method.
tableobjectA serializable object.
extraHeadersHeaderCollectionOptional extra headers to be attached to the request.

Returns:

typeDescription
PromisePromise with WebRequestResponse payload.

Usage

---@type HttpClient
local httpclient;

---@type string
local url;

---@type HttpMethod
local method;

---@type object
local table;

---@type HeaderCollection
local extraHeaders;


local val0 = httpclient.WebRequest(url, method, table, extraHeaders)

Example

You may pass custom header collection for the request. The web request returns a promise, which if resolved, returns an object of WebRequestResult type. You need to create a then function to handle the response from the promise. A promise may be rejected or result in an error.

local client = HttpClient();

-- simple get request, no content
client.WebRequest("http://foo.com/path/to/get", HttpMethod.GET).Then(
---@param result WebRequestResponse
function (result)
if result.StatusCode == 200 then
Debug:Log("Success");
end
end
);

local stringContent = "a string content";

-- POST request with string content
client.WebRequest("http://foo.com/path/to/post", HttpMethod.POST, stringContent).Then(
---@param result WebRequestResponse
function (result)
if result.StatusCode == 200 then
Debug:Log("Success");
end
end
);


local binaryContent = {};
local stringLength = string.len(stringContent);
for i = 1, stringLength, 1 do
binaryContent[i] = string.byte(stringContent, i);
end

-- POST request with binary content
client.WebRequest("http://foo.com/path/to/post", HttpMethod.POST, binaryContent).Then(
---@param result WebRequestResponse
function (result)
if result.StatusCode == 200 then
Debug:Log("Success");
end
end
);


local jsonContent = {
username = "fooBar",
password = "barFoo"
};

--- POST request with Json Content
client.WebRequest("http://foo.com/path/to/post", HttpMethod.POST, jsonContent).Then(
---@param result WebRequestResponse
function (result)
if result.StatusCode == 200 then
Debug:Log("Success");
end
end
);