Pre-Request Script
Pre-request script is a piece of code that will run before the execution of a request. Pre-request scripts gives users a chance to modify the request after variables have been resolved but before the request is made.
Example- Generating signatures for authentication
Pre-Request Script of Python
Pre-processing tasks including setting parameters, variable values, body data and headers can be performed using the pre-request script. Pre-request scripts can also be used to debug the code, for example, by logging output to the console. Additionally, we may obtain the result of the function, such as the date, time, timestamp, etc., utilising the pre-request script notion.
The code that is run prior to sending an HTTP request using an application like requests is known as the "pre-request" script in Python. Before submitting the actual request, the pre-request script is used to change the response fields or headers.
Below are the various examples of Pre-Request Scripts
Amazon SP API
Method - POST
# Pre-request Script for Amazon SP API - POST Method
# Import necessary libraries
import time
import datetime
import hashlib
import hmac
import json
# Provide your access and secret keys, host, endpoint, and other required values
access_key = '{{access_key}}'
secret_key = '{{secret_key}}'
host = '{{host}}'
endpoint = '{{hostname}}'
canonical_uri = '{{canonical_uri}}'
body = {{body}}
# Convert the request body to JSON format
request_parameters = json.dumps(body)
# Get current UTC time
t = datetime.datetime.utcnow()
amzdate = t.strftime('%Y%m%dT%H%M%SZ')
datestamp = t.strftime('%Y%m%d')
# Define request parameters
method = 'POST'
service = 'execute-api'
region = 'us-east-1'
canonical_querystring = ''
canonical_headers = 'host:' + host + '\\n' + 'x-amz-date:' + amzdate + '\\n'
signed_headers = 'host;x-amz-date'
# Calculate payload hash
payload_hash = hashlib.sha256((request_parameters).encode('utf-8')).hexdigest()
# Construct the canonical request
canonical_request = method + '\\n' + canonical_uri + '\\n' + canonical_querystring + '\\n' + canonical_headers + '\\n' + signed_headers + '\\n' + payload_hash
# Define algorithm and credential scope
algorithm = 'AWS4-HMAC-SHA256'
credential_scope = datestamp + '/' + region + '/'+ service + '/' + 'aws4_request'
# Calculate string to sign
string_to_sign = algorithm + '\\n' + amzdate + '\\n' + credential_scope + '\\n' + hashlib.sha256(canonical_request.encode('utf-8')).hexdigest()
# Generate signing key
kDate = hmac.new(('AWS4' + secret_key).encode('utf-8'), datestamp.encode('utf-8'), hashlib.sha256).digest()
kRegion = hmac.new(kDate, region.encode('utf-8'), hashlib.sha256).digest()
kService = hmac.new(kRegion, service.encode('utf-8'), hashlib.sha256).digest()
kSigning = hmac.new(kService, 'aws4_request'.encode('utf-8'), hashlib.sha256).digest()
signing_key = kSigning
# Calculate the signature
signature = hmac.new(signing_key, (string_to_sign).encode('utf-8'), hashlib.sha256).hexdigest()
# Construct the authorization header
authorization_header = algorithm + ' ' + 'Credential=' + access_key + '/' + credential_scope + ', ' + 'SignedHeaders=' + signed_headers + ', ' + 'Signature=' + signature
Method - POST (with Body)
# Pre-request Script for Amazon SP API - POST Method (with Body)
# Import necessary libraries
import time
import datetime
import hashlib
import hmac
import json
# Provide your access and secret keys, method, service, host, region, endpoint, and request body
access_key = 'XXXXXXXXXXXXXXXXXX'
secret_key = 'XXXXXXXXXXXXXXXXXX'
method = 'POST'
service = 'execute-api'
host = 'sellingpartnerapi-na.amazon.com'
region = 'us-east-1'
endpoint = 'https://sellingpartnerapi-na.amazon.com'
body = {
'reportType': 'GET_FLAT_FILE_ALL_ORDERS_DATA_BY_ORDER_DATE_GENERAL',
'dataStartTime': '{%yesterday%}T00:00:01',
'dataEndTime': '{%yesterday%}T23:59:59',
'marketplaceIds': ['XXXXXXXXXX']
}
# Convert the request body to JSON format
request_parameters = json.dumps(body)
# Get current UTC time
t = datetime.datetime.utcnow()
amzdate = t.strftime('%Y%m%dT%H%M%SZ')
datestamp = t.strftime('%Y%m%d')
# Define canonical URI, query string, and headers
canonical_uri = '/reports/2021-06-30/reports'
canonical_querystring = ''
canonical_headers = 'host:' + host + '\\n' + 'x-amz-date:' + amzdate + '\\n'
signed_headers = 'host;x-amz-date'
# Calculate payload hash
payload_hash = hashlib.sha256((request_parameters).encode('utf-8')).hexdigest()
# Construct the canonical request
canonical_request = method + '\\n' + canonical_uri + '\\n' + canonical_querystring + '\\n' + canonical_headers + '\\n' + signed_headers + '\\n' + payload_hash
# Define algorithm and credential scope
algorithm = 'AWS4-HMAC-SHA256'
credential_scope = datestamp + '/' + region + '/'+ service + '/' + 'aws4_request'
# Calculate string to sign
string_to_sign = algorithm + '\\n' + amzdate + '\\n' + credential_scope + '\\n' + hashlib.sha256(canonical_request.encode('utf-8')).hexdigest()
# Generate signing key
kDate = hmac.new(('AWS4' + secret_key).encode('utf-8'), datestamp.encode('utf-8'), hashlib.sha256).digest()
kRegion = hmac.new(kDate, region.encode('utf-8'), hashlib.sha256).digest()
kService = hmac.new(kRegion, service.encode('utf-8'), hashlib.sha256).digest()
kSigning = hmac.new(kService, 'aws4_request'.encode('utf-8'), hashlib.sha256).digest()
signing_key = kSigning
# Calculate the signature
signature = hmac.new(signing_key, (string_to_sign).encode('utf-8'), hashlib.sha256).hexdigest()
# Construct the authorization header
authorization_header = algorithm + ' ' + 'Credential=' + access_key + '/' + credential_scope + ', ' + 'SignedHeaders=' + signed_headers + ', ' + 'Signature=' + signature
Method - GET
# Pre-request Script for Amazon SP API - GET Method
# Import necessary libraries
import datetime
import hashlib
import hmac
# Provide your host, endpoint, access key, secret key, and canonical URI
host = '{{host}}' # Provide Values
endpoint = '{{hostname}}' # Provide Values
access_key = '{{access_key}}' # Provide Values
secret_key = '{{secret_key}}' # Provide Values
canonical_uri = '{{canonical_uri}}' # Provide Values
# Get current UTC time
t = datetime.datetime.utcnow()
amzdate = t.strftime('%Y%m%dT%H%M%SZ')
datestamp = t.strftime('%Y%m%d')
# Define method, service, region, canonical headers, and signed headers
method = 'GET'
service = 'execute-api'
region = 'us-east-1'
canonical_headers = 'host:' + host + '\\n' + 'x-amz-date:' + amzdate + '\\n'
signed_headers = 'host;x-amz-date'
# Calculate payload hash (empty for GET requests)
payload_hash = hashlib.sha256(('').encode('utf-8')).hexdigest()
# Construct the canonical request
canonical_request = method + '\\n' + canonical_uri + '\\n' +canonical_headers + '\\n' + signed_headers + '\\n' + payload_hash
# Define algorithm and credential scope
algorithm = 'AWS4-HMAC-SHA256'
credential_scope = datestamp + '/' + region + '/' + service + '/' + 'aws4_request'
# Calculate string to sign
string_to_sign = algorithm + '\\n' + amzdate + '\\n' + credential_scope + '\\n' + hashlib.sha256(canonical_request.encode('utf-8')).hexdigest()
# Generate signing key
kDate = hmac.new(('AWS4' + secret_key).encode('utf-8'), datestamp.encode('utf-8'), hashlib.sha256).digest()
kRegion = hmac.new(kDate, region.encode('utf-8'), hashlib.sha256).digest()
kService = hmac.new(kRegion, service.encode('utf-8'), hashlib.sha256).digest()
kSigning = hmac.new(kService, 'aws4_request'.encode('utf-8'), hashlib.sha256).digest()
signing_key = kSigning
# Calculate the signature
signature = hmac.new(signing_key, (string_to_sign).encode('utf-8'), hashlib.sha256).hexdigest()
# Construct the authorization header
authorization_header = algorithm + ' ' + 'Credential=' + access_key + '/' + credential_scope + ', ' + 'SignedHeaders=' + signed_headers + ', ' + 'Signature=' + signature
Oracle Netsuite
Source
import datetime
import random
import string
import hashlib
import base64
import hmac
import urllib
oauth_consumer_id = '{{consumer_id}}' # Provide Values
oauth_consumer_key = '{{consumer_key}}' # Provide Values
oauth_consumer_secret ='{{consumer_secret}}' # Provide Values
oauth_token ='{{token}}' # Provide Values
oauth_token_secret ='{{token_secret}}' # Provide Values
###########################################################################################################################################################
request_method = 'POST'
url = 'https://{{hostname}}.suitetalk.api.netsuite.com/services/rest/query/v1/suiteql'
oauth_signature_method = 'HMAC-SHA256'
oauth_timestamp = str(int(datetime.datetime.now().timestamp()))
oauth_nonce = ''.join(random.choices(string.ascii_letters + string.digits, k = 11))
oauth_version = '1.0'
normalized_request_method = request_method.replace(' ', '')
normalized_string_url = urllib.parse.quote(url, safe = '')
normalized_params = {'oauth_consumer_key': oauth_consumer_key,'oauth_token': oauth_token,'oauth_signature_method': oauth_signature_method,'oauth_timestamp': oauth_timestamp,'oauth_nonce': oauth_nonce,'oauth_version': oauth_version,'limit':max_num,'offset':min_num}
sorted_params = dict(sorted(normalized_params.items()))
normalized_string_parmas = [k+'='+v for k,v in sorted_params.items()]
normalized_string_parmas = '&'.join([str(elem) for elem in normalized_string_parmas])
normalized_string_parmas.replace(' ','')
normalized_string_parmas = urllib.parse.quote(normalized_string_parmas, safe = '')
base_string = request_method + '&' + normalized_string_url + '&' + normalized_string_parmas
base_string = str.encode(base_string)
signature_key = oauth_consumer_secret + '&' + oauth_token_secret
signature_key = str.encode(signature_key)
oauth_signature = hmac.new(signature_key, base_string, hashlib.sha256)
oauth_signature.hexdigest()
oauth_signature = base64.b64encode(oauth_signature.digest())
oauth_signature = oauth_signature.decode('UTF-8')
oauth_signature = urllib.parse.quote(oauth_signature, safe = '')
signature ='OAuth realm="'f'{oauth_consumer_id}",oauth_consumer_key="'f'{oauth_consumer_key}",oauth_token="'f'{oauth_token}",oauth_signature_method="'f'{oauth_signature_method}",oauth_timestamp="'f'{oauth_timestamp}",oauth_nonce="'f'{oauth_nonce}",oauth_version="'f'{oauth_version}",oauth_signature="'f'{oauth_signature}"'
Target / Operations
import os
import requests
import time
import hashlib
import hmac
import base64
# Set your environment variables (replace with your actual credentials)
account = '{{account}}'
consumerKey = '{{consumerKey}}'
consumerSecret = '{{consumerSecret}}'
tokenId = '{{tokenId}}'
tokenSecret = '{{tokenSecret}}'
# Generate timestamp and nonce
timestamp = str(int(time.time()))
nonce = ''.join([str(os.urandom(1)) for _ in range(11)])
# Create base string
baseString = f"{account}&{consumerKey}&{tokenId}&{nonce}&{timestamp}"
# Create key
key = f"{consumerSecret}&{tokenSecret}"
# Create signature
signature = base64.b64encode(hmac.new(key.encode('utf-8'), baseString.encode('utf-8'), hashlib.sha256).digest()).decode('utf-8')
Azure Cosmos DB
Source & Target / Operations
# Pre-request Script for Azure Cosmos DB - Source & Target / Operations
# Import necessary libraries
from wsgiref.handlers import format_date_time
from datetime import datetime
from time import mktime
import base64
from urllib.parse import quote
import hmac
from hashlib import sha256
# Provide your endpoint URL, master key, resource type, and resource ID
endpoint_url = '{{hostname}}' # Provide Values
master_key = '{{master_key}}' # Provide Values
resource_type = '{{resource_type}}' # Provide Values
resource_id = '{{resource_id}}' # Provide Values
# Decode the master key
key = base64.b64decode(master_key)
# Define endpoint method (e.g., post), current date, and format the date
endpoint_method = 'post'
now = datetime.now()
stamp = mktime(now.timetuple())
date = format_date_time(stamp)
# Construct the string to be signed
text = '{endpoint_method}\\n{resource_type}\\n{resource_id}\\n{date}\\n{other}\\n'.format(
endpoint_method=(endpoint_method.lower() or ''),
resource_type=(resource_type.lower() or ''),
resource_id=(resource_id or ''),
date=date.lower(),
other=''.lower()
)
# Encode the text to bytes
body = text.encode('utf-8')
# Generate HMAC digest
digest = hmac.new(key, body, sha256).digest()
# Encode the digest to base64 and decode to UTF-8
signature = base64.encodebytes(digest).decode('utf-8')
# Define key type and version
key_type = 'master'
version = '1.0'
# Construct the URI
uri = f'type={key_type}&ver={version}&sig={signature[:-1]}'
# URL encode the URI
authorization = quote(uri)
Amazon S3
Method : PUT
Note: This pre_request_script is for loading data to Amazon S3 Bucket.
# Pre-request Script for Amazon S3 - Method: PUT
# Import necessary libraries
import hashlib
import hmac
import datetime
# Provide your access key, secret key, bucket name, region, payload, host, and canonical URI
access_key = '{{access_key}}' # Provide Values
secret_key = '{{secret_key}}' # Provide Values
bucket = '{{bucket_name}}' # Provide Values
region = '{{region}}' # Provide Values
payload = '''{{payload}}''' # Provide Values
host = '{{host}}' # Provide Values
canonical_uri = '/{{canonical_uri}}' # Provide Values
# Define HTTP method
method = 'PUT'
# Get current UTC time and format it
amzdate = datetime.datetime.utcnow().strftime('%Y%m%dT%H%M%SZ')
datestamp = datetime.datetime.utcnow().strftime('%Y%m%d')
# Define canonical query string, payload hash, canonical headers, and signed headers
canonical_querystring = ''
payload_hash = hashlib.sha256(payload.encode()).hexdigest()
canonical_headers = 'host:' + host + '\\n' + 'x-amz-content-sha256:' + payload_hash + '\\n' + 'x-amz-date:' + amzdate + '\\n'
signed_headers = 'host;x-amz-content-sha256;x-amz-date'
# Construct the canonical request
canonical_request = method + '\\n' + canonical_uri + '\\n' + canonical_querystring + '\\n' + canonical_headers + '\\n' + signed_headers + '\\n' + hashlib.sha256(payload.encode()).hexdigest()
# Define algorithm and credential scope
algorithm = 'AWS4-HMAC-SHA256'
credential_scope = datestamp + '/' + region + '/s3/aws4_request'
# Construct string to sign
string_to_sign = algorithm + '\\n' + amzdate + '\\n' + credential_scope + '\\n' + hashlib.sha256(canonical_request.encode()).hexdigest()
# Derive signing key
date_key = hmac.new(('AWS4' + secret_key).encode(), datestamp.encode(), hashlib.sha256).digest()
region_key = hmac.new(date_key, region.encode(), hashlib.sha256).digest()
service_key = hmac.new(region_key, 's3'.encode(), hashlib.sha256).digest()
signing_key = hmac.new(service_key, 'aws4_request'.encode(), hashlib.sha256).digest()
# Calculate the signature
signature = hmac.new(signing_key, string_to_sign.encode(), hashlib.sha256).hexdigest()
# Construct the authorization header
authorization_header = algorithm + ' Credential=' + access_key + '/' + credential_scope + ', SignedHeaders=' + signed_headers + ', Signature=' + signature
Method : GET
Note: This pre_request_script is for retrieving data from Amazon S3 Bucket.
# Pre-request Script for Amazon S3 - Method: GET
# Import necessary libraries
import hashlib
import hmac
import datetime
# Provide your access key, secret key, bucket name, region, host, and canonical URI
access_key = '{{access_key}}' # Provide Values
secret_key = '{{secret_key}}' # Provide Values
bucket = '{{bucket_name}}' # Provide Values
region = '{{region}}' # Provide Values
host = '{{host}}' # Provide Values
canonical_uri = '/{{canonical_uri}}' # Provide Values
# Define HTTP method and service
method = 'GET'
service = 's3'
# Get current UTC time and format it
t = datetime.datetime.utcnow()
amzdate = t.strftime('%Y%m%dT%H%M%SZ')
datestamp = t.strftime('%Y%m%d')
# Define canonical query string, canonical headers, and signed headers
canonical_querystring = ''
canonical_headers = 'host:' + host + '\n' + 'x-amz-date:' + amzdate + '\n'
signed_headers = 'host;x-amz-date'
# Calculate payload hash (empty for GET requests)
payload_hash = hashlib.sha256(('').encode('utf-8')).hexdigest()
# Construct the canonical request
canonical_request = method + '\n' + canonical_uri + '\n' + canonical_querystring + '\n' + canonical_headers + '\n' + signed_headers + '\n' + payload_hash
# Define algorithm and credential scope
algorithm = 'AWS4-HMAC-SHA256'
credential_scope = datestamp + '/' + region + '/'+ service + '/' + 'aws4_request'
# Construct string to sign
string_to_sign = algorithm + '\n' + amzdate + '\n' + credential_scope + '\n' + hashlib.sha256(canonical_request.encode('utf-8')).hexdigest()
# Derive signing key
date_key = hmac.new(("AWS4" + secret_key).encode(), datestamp.encode(), hashlib.sha256).digest()
region_key = hmac.new(date_key, region.encode(), hashlib.sha256).digest()
service_key = hmac.new(region_key, "s3".encode(), hashlib.sha256).digest()
signing_key = hmac.new(service_key, "aws4_request".encode(), hashlib.sha256).digest()
# Calculate the signature
signature = hmac.new(signing_key, (string_to_sign).encode('utf-8'), hashlib.sha256).hexdigest()
# Construct the authorization header
authorization_header = algorithm + ' ' + 'Credential=' + access_key + '/' + credential_scope + ', ' + 'SignedHeaders=' + signed_headers + ', ' + 'Signature=' + signature