Skip to content

Instantly share code, notes, and snippets.

@vinovator
Last active October 2, 2015 14:04
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 vinovator/2550e503a9b1b8738a87 to your computer and use it in GitHub Desktop.
Save vinovator/2550e503a9b1b8738a87 to your computer and use it in GitHub Desktop.
A sample restful client for POST operation - assumes digest authentication
# RestfulPostClient.py
# Python 2.7.6
import requests
from requests.auth import HTTPDigestAuth
# import json # Json module is not required as we are directly passing json to requests
# Replace with the correct URL
url = "http://api_url"
# Replace with appropriate header
header = {"content-type": "application/json"}
# Replace with correct payload
payload = {"some_key": "some_data"}
myResponse = requests.post(url,
# data = json.dumps(payload), # takes a dict and connverts into json
json = payload # directly pass a dict, it will be auto-converted to json
headers = header,
auth=HTTPDigestAuth(raw_input("username: "), raw_input("Password: ")))
if(myResponse.ok):
print (myResponse.status_code, myResponse.reason, " Your post operation was successful")
else:
# If response code is not ok (200), print the resulting http error code with description
myResponse.raise_for_status()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment