Skip to content

Commit ff15681

Browse files
Luke-Roy-IBMreggeenr
authored andcommitted
add additonal http example for python using urllib
Signed-off-by: Luke Roy <luke.roy@ibm.com>
1 parent 547aa6a commit ff15681

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

File renamed without changes.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from urllib.request import urlopen, Request
2+
from urllib import parse
3+
import json
4+
5+
6+
def main(params):
7+
8+
url = "https://httpbin.org"
9+
request = None
10+
11+
if params["__ce_method"] == "POST":
12+
url = url + "/post"
13+
data = json.dumps(params).encode()
14+
request = Request(url, data=data, method='POST')
15+
else:
16+
url = url + "/get"
17+
request = Request(url, method='GET')
18+
19+
20+
request.add_header('Content-Type', 'application/json')
21+
response = urlopen(request)
22+
data = response.read()
23+
24+
dictData = json.loads(data)
25+
26+
return {
27+
"headers": {
28+
"Content-Type": "application/json",
29+
},
30+
"statusCode": response.status,
31+
"body": dictData
32+
}

0 commit comments

Comments
 (0)