This repository has been archived by the owner on Feb 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRequests.lua
204 lines (168 loc) · 4.36 KB
/
Requests.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
local httpService = game:GetService("HttpService")
local requests = {}
-- Functions
local function getRawCookie(cookie) -- Removes warning from cookie
local modifiedCookie = string.gsub(cookie, "-", " ")
if string.find(modifiedCookie, "DO NOT SHARE THIS") then
return string.sub(cookie, 117, #cookie)
else
return cookie
end
end
local function removeLastChar(str)
return string.sub(str, 0, #str - 1)
end
local function cookiesToHeader(cookies)
local headersStr = ""
for key, value in pairs(cookies) do
local template = "%s=%s&"
local cookieStr = string.format(template, key, getRawCookie(value))
headersStr = headersStr .. cookieStr
end
headersStr = removeLastChar(headersStr)
return headersStr
end
local function mergeTables(...)
local newTbl = {}
for _, t in pairs(table.pack(...)) do
for _, v in pairs(t) do
table.insert(newTbl, v)
end
end
return newTbl
end
local function isJSON(str)
local _, err = pcall(function()
httpService:JSONDecode(str)
end)
if err then
return false
else
return true
end
end
local function trustBypass(url)
return string.gsub(url, "roblox.com", "rprxy.xyz")
end
-- Module functions
function requests:SendRequest(requestOptions)
-- requestOptions: url, method, cookies, headers
-- Errors
if not requestOptions then
error("No requestOptions provided")
end
if not requestOptions.url then
error("No URL provided")
end
if not requestOptions.method then
error("No method provided")
end
-- Defaults
if not requestOptions.cookies then
requestOptions.cookies = {}
end
if not requestOptions.headers then
requestOptions.headers = {}
end
requestOptions.method = string.upper(requestOptions.method) -- Make method uppercase
requestOptions.url = trustBypass(requestOptions.url) -- Allow access to roblox sites
-- Setup cookies
requestOptions.headers['Cookie'] = cookiesToHeader(requestOptions.cookies)
-- Send request
local response = {
statusCode = 200 -- Default status code
}
if requestOptions.method == "GET" then
response = {}
local res
local _, httpErr = pcall(function()
res = httpService:GetAsync(requestOptions.url, false, requestOptions.headers)
end)
-- Auto convert json
if isJSON(res) then
response.data = httpService:JSONDecode(res)
else
response.data = res
end
-- Set statusCode
if httpErr then
response.statusCode = string.match(httpErr, "%d+") -- Get just the status code number
error(httpErr)
end
elseif requestOptions.method == "POST" then
-- Errors
if not requestOptions.data then
error("No POST data provided")
end
-- Defaults
if requestOptions.contentType == nil then
requestOptions.contentType = Enum.HttpContentType.ApplicationJson -- Default
end
-- Convert dictionary to json
if not isJSON(requestOptions.data) then
if type(requestOptions.data) == "table" then
requestOptions.data = httpService:JSONEncode(requestOptions.data)
end
end
-- Send request
local _, httpErr = pcall(function()
response = httpService:PostAsync(requestOptions.url, requestOptions.data, requestOptions.contentType, false, requestOptions.headers)
end)
-- Set statusCode
if httpErr then
response.statusCode = string.match(httpErr, "%d+") -- Get just the status code number
error(httpErr)
end
end
return response
end
function requests:Get(requestOptions)
-- Errors
if not requestOptions then
error("No requestOptions provided")
end
if not requestOptions.url then
error("No URL provided")
end
-- Defaults
if not requestOptions.cookies then
requestOptions.cookies = {}
end
if not requestOptions.headers then
requestOptions.headers = {}
end
-- Send request
local response = requests:SendRequest({
url = requestOptions.url,
cookies = requestOptions.cookies,
headers = requestOptions.headers,
method = "GET"
})
return response
end
function requests:Post(requestOptions)
-- Errors
if not requestOptions then
error("No requestOptions provided")
end
if not requestOptions.url then
error("No URL provided")
end
-- Defaults
if not requestOptions.cookies then
requestOptions.cookies = {}
end
if not requestOptions.headers then
requestOptions.headers = {}
end
-- Send request
local response = requests:SendRequest({
url = requestOptions.url,
cookies = requestOptions.cookies,
headers = requestOptions.headers,
data = requestOptions.data,
method = "POST"
})
return response
end
return requests