forked from IBM/CodeEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurlopen.py
More file actions
31 lines (23 loc) · 727 Bytes
/
urlopen.py
File metadata and controls
31 lines (23 loc) · 727 Bytes
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
from urllib.request import urlopen, Request
import json
def main(params):
url = "https://httpbin.org"
request = None
if params["__ce_method"] == "POST":
url = url + "/post"
data = json.dumps(params).encode()
request = Request(url, data=data, method='POST')
else:
url = url + "/get"
request = Request(url, method='GET')
request.add_header('Content-Type', 'application/json')
response = urlopen(request)
data = response.read()
dictData = json.loads(data)
return {
"headers": {
"Content-Type": "application/json",
},
"statusCode": response.status,
"body": dictData
}