forked from open-runtimes/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.rb
More file actions
45 lines (41 loc) · 1.13 KB
/
Copy pathindex.rb
File metadata and controls
45 lines (41 loc) · 1.13 KB
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
require "httparty"
require "json"
def main(req, res)
begin
payload = JSON.parse(req.payload)
url = payload["url"]
method = payload["method"]
headers = payload["headers"]
body = payload["body"]
rescue Exception => err
puts err
raise 'Payload is invalid.'
end
if url.nil? or url.empty? or method.nil? or method.empty?
raise 'Payload is invalid.'
end
begin
response = HTTParty.send(method.downcase, url, headers: headers, body: body)
if response.success?
res.json({
"success": response.success?,
"response": {
"headers": response.headers,
"code": response.code,
"body": response.body
}
})
else
res.json({
"success": false,
"message": {
"code": response.code,
"body": response.message
}
})
end
rescue Exception => err
puts err
raise 'Request could not be made.'
end
end