Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- dogo8me2
- local HttpClient = {}
- local HttpService = game:GetService("HttpService")
- -- Function to make a GET request
- function HttpClient.getRequest(url: string, apiKey: string, headers: any): any
- local response
- local success, err = pcall(function()
- local headersArray = {["x-api-key"] = apiKey}
- if headers then
- for i,v in headers do
- headersArray[i] = v
- end
- end
- print(headersArray)
- response = HttpService:RequestAsync({
- Url = url,
- Method = "GET",
- Headers = headersArray
- })
- end)
- -- returning and error recognition
- if not success then warn(err) end
- local responseSuccess, responseErr = pcall(function()
- if not response.Success then error(response.StatusMessage) end
- end)
- if not responseSuccess then
- warn(responseErr)
- end
- return response
- end
- -- Function to make a POST request
- function HttpClient.postRequest(url: string, apiKey: string, data: any): any
- local response
- local success, err = pcall(function()
- response = HttpService:RequestAsync({
- Url = url,
- Method = "POST",
- Headers = {
- ["x-api-key"] = apiKey,
- ["Content-Type"] = "application/json"
- },
- Body = HttpService:JSONEncode(data)
- })
- end)
- if not success then error(err) end
- if response then return response else return nil end
- end
- -- Function to make a PATCH request
- function HttpClient.patchRequest(url: string, apiKey: string, data: any)
- local response
- local success, err = pcall(function()
- response = HttpService:RequestAsync({
- Url = url,
- Method = "PATCH",
- Headers = {
- ["x-api-key"] = apiKey,
- ["Content-Type"] = "application/json"
- },
- Body = HttpService:JSONEncode(data)
- })
- end)
- if not success then error(err) end
- if response then return response else return nil end
- end
- -- Function to make a DELETE request
- function HttpClient.deleteRequest(url: string, apiKey: string, data: any)
- local response
- local success, err = pcall(function()
- response = HttpService:RequestAsync({
- Url = url,
- Method = "DELETE",
- Headers = {
- ["x-api-key"] = apiKey,
- ["Content-Type"] = "application/json"
- },
- Body = HttpService:JSONEncode(data)
- })
- end)
- if not success then error(err) end
- if response then return response else return nil end
- end
- return HttpClient
Advertisement
Add Comment
Please, Sign In to add comment