Skip to content

Instantly share code, notes, and snippets.

@HalCanary
Created November 21, 2022 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HalCanary/a1f704274aaa40740949a094b42b3356 to your computer and use it in GitHub Desktop.
Save HalCanary/a1f704274aaa40740949a094b42b3356 to your computer and use it in GitHub Desktop.
rest.py
import urllib.request
import urllib.parse
import json
################################################################################
def Get(auth, host, path, query={}):
q = urllib.parse.urlencode(query)
req = urllib.request.Request(
f"https://{host}{path}?{q}",
headers={"Authorization": auth})
with urllib.request.urlopen(req) as r:
return json.loads(r.read())
def Post(auth, host, path, query={}):
req = urllib.request.Request(
f"https://{host}{path}",
data=bytes(json.dumps(query), 'UTF-8'),
headers={
"Authorization": auth,
"Content-Type": "application/json"})
with urllib.request.urlopen(req) as r:
return json.loads(r.read())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment