Skip to content

Instantly share code, notes, and snippets.

@vlad-bezden
Created August 8, 2019 14:12
Show Gist options
  • Save vlad-bezden/3a34d6895ab8ede43542533574a3d019 to your computer and use it in GitHub Desktop.
Save vlad-bezden/3a34d6895ab8ede43542533574a3d019 to your computer and use it in GitHub Desktop.
POST JSON data using Python urllib standard library
import urllib.request
import json
from pprint import pprint
url = "<some_url>"
values = {
"first_name": "Vlad",
"last_name": "Bezden",
"email": "[email protected]",
"phone": "+1 6095010607",
"urls": [
"https://www.linkedin.com/in/vlad-bezden-7950a51/",
"https://twitter.com/VladBezden",
"https://github.com/vlad-bezden",
],
}
headers = {
"Content-Type": "application/json",
"Accept": "application/json",
}
data = json.dumps(values).encode("utf-8")
pprint(data)
try:
req = urllib.request.Request(url, data, headers)
with urllib.request.urlopen(req) as f:
res = f.read().decode()
pprint(res)
except Exception as e:
pprint(e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment