Created
August 8, 2019 14:12
-
-
Save vlad-bezden/3a34d6895ab8ede43542533574a3d019 to your computer and use it in GitHub Desktop.
POST JSON data using Python urllib standard library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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