Skip to content

Instantly share code, notes, and snippets.

@fabianogoes
Last active August 30, 2018 14:06
Show Gist options
  • Save fabianogoes/19d815063336bf01cbd248e23df217ef to your computer and use it in GitHub Desktop.
Save fabianogoes/19d815063336bf01cbd248e23df217ef to your computer and use it in GitHub Desktop.
Example request POST by Python
#!python3
"""
requirements:
requests==2.19.1
Reference: http://docs.python-requests.org/en/master/user/quickstart/
"""
import requests
def request(list-dict):
try:
req_body = json.dumps(list-dict).encode()
autorization = "Basic {}".format(API_AUTH_HASH)
headers = {
'Authorization': autorization,
'Accept': 'application/json',
'Cache-Control': 'no-cache',
'Content-Type': 'application/json',
'Content-Length': str(len(req_body))
}
response = requests.post(API_URL, data=req_body, headers=headers)
if response.status_code not in (200, 201):
print('Response status_code: {}'.format(response.status_code))
except Exception as e:
print('Request Error: {}'.format(e))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment