Skip to content

Instantly share code, notes, and snippets.

@abitrolly
abitrolly / postdata.py
Last active January 28, 2024 12:00
POST JSON with Python3 urllib
# Public domain
from urllib import request
def post_data(url, data, headers={'Content-Type':'application/json'}):
"""
POST data string to `url`, return page and headers
"""
# if data is not in bytes, convert to it to utf-8 bytes
bindata = data if type(data) == bytes else data.encode('utf-8')