Python – PUT data to a URL (Edit docusign recipient)

Wanted to submit a PUT request to a URL. The requests module in Python is the one recommended to do so. Can be easily downloaded using:

pip install requests


easy_install requests

More ways to install:

Post installation, here’s a snippet to PUT data to a URL (this specific example shows how to Edit an existing docusign recipient):

import sys, json, urllib, urllib2, requests
import base64
# enter your info:
username = ""
password = "YouP@ssword"
integratorKey = "YOUR-integration-key"

authenticateStr = "<DocuSignCredentials>" \
                    "<Username>" + username + "</Username>" \
                    "<Password>" + password + "</Password>" \
                    "<IntegratorKey>" + integratorKey + "</IntegratorKey>" \

# STEP 1 - Login
url = ''
payload = {
  "signers" :
      "email": "",
      "name": "New Recipient",
      "recipientId": "2"
headers = {'X-DocuSign-Authentication': authenticateStr, 'Accept': 'application/json'}
req = requests.put(url, data = json.dumps(payload), headers=headers)
print req
print req.status_code
print req.content

apt-get update errors on debian server – signature couldn’t be verified because the public key is not available

I was running into Warnings and Errors while executing an apt-get update on a debian server.

W: GPG error: squeeze Release: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY AED4B06F473041FA NO_PUBKEY 64481591B98321F9
W: GPG error: squeeze/updates Release: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY AED4B06F473041FA
W: GPG error: squeeze-updates Release: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 8B48AD6246925553

In computers based on a Debian operating system that uses Linux kernel, error messages may come up like ‘NO_PUBKEY’.This happens while using the Apt-Get command line tool and this error is associated with the tool’s update feature. The new feature in the Apt-Get package management tool guarantees the authenticity of the server before updating the Debian OS. That’s why the error ‘NO_PUBKEY’ pops up. This problem can be solved by keying in the appropriate commands. The fix for this is to re-download the keys using the hexidecimal numbers given in the error.

apt-key adv --keyserver --recv-keys 64481591B98321F9 AED4B06F473041FA 8B48AD6246925553

You should see a similar looking out put

Executing: gpg --ignore-time-conflict --no-options --no-default-keyring --secret-keyring /etc/apt/secring.gpg --trustdb-name /etc/apt/trustdb.gpg --keyring /etc/apt/trusted.gpg --primary-keyring /etc/apt/trusted.gpg --keyserver --recv-keys 64481591B98321F9 AED4B06F473041FA 8B48AD6246925553

Postgres login without password

In order to login to postgres without a password, you need to create a .pgpass file in your home directory. The contents of the file should look similar to:


After creating the file, make sure that the file permissions are 600 or less

chmod 600 ~/.pgpass

After the file is setup, you can login to the database just by executing the following command (or alternatively setup an alias in your bash file to that would execute the command. If you’re applying shortcuts, why not get maximum results):

psql -hlocalhost -Uotheruser other_db

jQuery – bind checkbox and text input such that input allowed only if box is checked

Using jQuery to bind together a checkbox and input text field. The text input is only editable if the check box is selected. Otherwise, reset the textbox value and make it readonly.

	var $objFSM = $( "#id_file_size_max" ); // Checkbox
	var $objMAFS = $( "#id_max_allowed_file_size" ); // Input TEXT
	// Check checked property of FSM and set readonly property of MAFS accordingly on form load
	if($objFSM.attr('checked')) { // Make MAFS editable
		$objMAFS.attr('readonly', false);
	else { // Make MAFS readonly
		$objMAFS.attr('readonly', true);
	$ {
		if($objFSM.attr('checked')) { // Make MAFS editable
			$objMAFS.attr('readonly', false);
		else { // Make MAFS readonly
			$objMAFS.attr('readonly', true);

PHP – Validate JSON

Here’s what I did to validate if the JSON is valid. I found tutorials on using json_last_error(), but I found this approach to be much simpler.

$arr_license = json_decode($_POST['json_license'], true);
  exit('{"ReturnType":"Error", "ReturnCode":"101", "ReturnMessage":"json_license must be a valid JSON string."}');
//-- Proceed from here with valid $arr_license

PHP API Framework supporting multiple functions and arguments

Here’s a scalable framework to make API’s that support multiple functions. Each function in turn has mandatory and/or optional parameters. The parameters are combined to create a hash for security and it’s dynamically generated based on the function definition.

define('SECRET', 'p24gvszaox6a1b2c392uewkyhezyqwk9');

class ApiFuncs {
	const FileDownload			=	'file-download';
	const FileAdd				=	'file-add';
	const MailSend				=	'mail-send';
	const FoldersList			=	'folders-list';
	const LastModFileFromFolder		=	'getLastModFileFromFolder';
	const AllFilesFromFolder		=	'getAllFilesFromFolder';

$INPUT_PARAMS = array (
	ApiFuncs::FileDownload	=> array('mandatory' => array('fileId')),
	ApiFuncs::FileAdd	=> array('mandatory' => array('fileContent','fileName','folderId')),
	ApiFuncs::MailSend	=> array('mandatory' => array('csv_fileIds', 'to', 'subject'),
							'optional' => array('message', 'cc', 'bcc')),
	ApiFuncs::FoldersList	=> array('mandatory' => array('folderId')),
	ApiFuncs::LastModFileFromFolder	=> array('mandatory' => array('folderId'),
						'optional' => array('fileExtension')),
	ApiFuncs::AllFilesFromFolder	=> array('mandatory' => array('folderId'),
						'optional' => array('fileExtension')),


//-- API Functions

function validate() {
	global $_POST, $INPUT_PARAMS;
	try {
		if(!array_key_exists('function', $_POST) || !array_key_exists($_POST['function'], $INPUT_PARAMS)) {
			exit(json_encode(getSimpleDictResponse(ReturnType::Error, ReturnCode::InvalidFunction)));
		$token_params = $INPUT_PARAMS[$_POST['function']]['mandatory'];
		foreach($token_params as $key => $value) {
			if(!array_key_exists($value, $_POST)) {
				exit(json_encode(getSimpleDictResponse(ReturnType::Error, ReturnCode::MissingApiParam, $value)));
	catch(Exception $e){
		exit(json_encode(getSimpleDictResponse(ReturnType::Error, ReturnCode::InvalidAPICall, $e)));

function validate_values() {
	global $_POST, $INPUT_PARAMS;
	switch($_POST['function']) {
		case ApiFuncs::FileDownload:
			if(!preg_match('/^\d+$/',$_POST['fileId'])) {
				exit(json_encode(getSimpleDictResponse(ReturnType::Error, ReturnCode::InvalidFileId, $_POST['fileId'])));

function verify_token() {
	$token_params = $INPUT_PARAMS[$_POST['function']]['mandatory'];
	$hash = "function=".$_POST['function'];
	foreach($token_params as $key => $value) {
		$hash .= $value.'='.$_POST[$value];
	$hash .= "secret=".SECRET;
	if(getSHA1Token($hash) != $_POST['token']) {
		exit(json_encode(getSimpleDictResponse(ReturnType::Error, ReturnCode::InvalidToken)));