Guest User

HttpClient

a guest
Mar 19th, 2025
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.26 KB | None | 0 0
  1. -- dogo8me2
  2. local HttpClient = {}
  3. local HttpService = game:GetService("HttpService")
  4.  
  5. -- Function to make a GET request
  6. function HttpClient.getRequest(url: string, apiKey: string, headers: any): any
  7.     local response
  8.     local success, err = pcall(function()
  9.         local headersArray = {["x-api-key"] = apiKey}
  10.         if headers then
  11.             for i,v in headers do
  12.                 headersArray[i] = v
  13.             end
  14.         end
  15.         print(headersArray)
  16.         response = HttpService:RequestAsync({
  17.             Url = url,
  18.             Method = "GET",
  19.             Headers = headersArray
  20.         })
  21.     end)
  22.     -- returning and error recognition
  23.     if not success then warn(err) end
  24.     local responseSuccess, responseErr = pcall(function()
  25.         if not response.Success then error(response.StatusMessage) end
  26.     end)
  27.     if not responseSuccess then
  28.         warn(responseErr)
  29.     end
  30.     return response
  31. end
  32.  
  33. -- Function to make a POST request
  34. function HttpClient.postRequest(url: string, apiKey: string, data: any): any
  35.     local response
  36.     local success, err = pcall(function()
  37.         response = HttpService:RequestAsync({
  38.             Url = url,
  39.             Method = "POST",
  40.             Headers = {
  41.                 ["x-api-key"] = apiKey,
  42.                 ["Content-Type"] = "application/json"
  43.             },
  44.             Body = HttpService:JSONEncode(data)
  45.         })
  46.     end)
  47.     if not success then error(err) end
  48.     if response then return response else return nil end
  49. end
  50.  
  51. -- Function to make a PATCH request
  52. function HttpClient.patchRequest(url: string, apiKey: string, data: any)
  53.     local response
  54.     local success, err = pcall(function()
  55.         response = HttpService:RequestAsync({
  56.             Url = url,
  57.             Method = "PATCH",
  58.             Headers = {
  59.                 ["x-api-key"] = apiKey,
  60.                 ["Content-Type"] = "application/json"
  61.             },
  62.             Body = HttpService:JSONEncode(data)
  63.         })
  64.     end)
  65.     if not success then error(err) end
  66.     if response then return response else return nil end
  67. end
  68.  
  69. -- Function to make a DELETE request
  70. function HttpClient.deleteRequest(url: string, apiKey: string, data: any)
  71.     local response
  72.     local success, err = pcall(function()
  73.         response = HttpService:RequestAsync({
  74.             Url = url,
  75.             Method = "DELETE",
  76.             Headers = {
  77.                 ["x-api-key"] = apiKey,
  78.                 ["Content-Type"] = "application/json"
  79.             },
  80.             Body = HttpService:JSONEncode(data)
  81.         })
  82.     end)
  83.     if not success then error(err) end
  84.     if response then return response else return nil end
  85. end
  86. return HttpClient
Advertisement
Add Comment
Please, Sign In to add comment