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
import time
import datetime, hashlib, hmac
import json
access_key='{{access_key}}' # Provide Values
secret_key='{{secret_key}}' # Provide Values
host = '{{host}}' # Provide Values
endpoint = '{{hostname}}' # Provide Values
canonical_uri = '{{canonical_uri}}' # Provide Values
body = {{body}} # Provide Values
##########################################################################################################################################################
request_parameters =json.dumps(body)
t = datetime.datetime.utcnow()
amzdate = t.strftime('%Y%m%dT%H%M%SZ')
datestamp = t.strftime('%Y%m%d')
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'
payload_hash = hashlib.sha256((request_parameters).encode('utf-8')).hexdigest()
canonical_request = method + '\\n' + canonical_uri + '\\n' + canonical_querystring + '\\n' + canonical_headers + '\\n' + signed_headers + '\\n' + payload_hash
algorithm = 'AWS4-HMAC-SHA256'
credential_scope = datestamp + '/' + region + '/'+ service + '/' + 'aws4_request'
string_to_sign = algorithm + '\\n' + amzdate + '\\n' + credential_scope + '\\n' + hashlib.sha256(canonical_request.encode('utf-8')).hexdigest()
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
signature = hmac.new(signing_key, (string_to_sign).encode('utf-8'), hashlib.sha256).hexdigest()
authorization_header = algorithm + ' ' + 'Credential=' + access_key + '/' + credential_scope + ', ' + 'SignedHeaders=' + signed_headers + ', ' + 'Signature=' + signature
Method - POST (with Body)
import time
import datetime, hashlib, hmac
import json
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']}
request_parameters =json.dumps(body)
t = datetime.datetime.utcnow()
amzdate = t.strftime('%Y%m%dT%H%M%SZ')
datestamp = t.strftime('%Y%m%d')
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'
payload_hash = hashlib.sha256((request_parameters).encode('utf-8')).hexdigest()
canonical_request = method + '\\n' + canonical_uri + '\\n' + canonical_querystring + '\\n' + canonical_headers + '\\n' + signed_headers + '\\n' + payload_hash
algorithm = 'AWS4-HMAC-SHA256'
credential_scope = datestamp + '/' + region + '/'+ service + '/' + 'aws4_request'
string_to_sign = algorithm + '\\n' + amzdate + '\\n' + credential_scope + '\\n' + hashlib.sha256(canonical_request.encode('utf-8')).hexdigest()
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
signature = hmac.new(signing_key, (string_to_sign).encode('utf-8'), hashlib.sha256).hexdigest()
authorization_header = algorithm + ' ' + 'Credential=' + access_key + '/' + credential_scope + ', ' + 'SignedHeaders=' + signed_headers + ', ' + 'Signature=' + signature
Method - GET
import datetime, hashlib, hmac
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
##########################################################################################################################################################
t = datetime.datetime.utcnow()
amzdate = t.strftime('%Y%m%dT%H%M%SZ')
datestamp = t.strftime('%Y%m%d')
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'
payload_hash = hashlib.sha256(('').encode('utf-8')).hexdigest()
canonical_request = method + '\\n' + canonical_uri + '\\n' +canonical_headers + '\\n' + signed_headers + '\\n' + payload_hash
algorithm = 'AWS4-HMAC-SHA256'
credential_scope = datestamp + '/' + region + '/' + service + '/' + 'aws4_request'
string_to_sign = algorithm + '\\n' + amzdate + '\\n' + credential_scope + '\\n' + hashlib.sha256(canonical_request.encode('utf-8')).hexdigest()
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
signature = hmac.new(signing_key, (string_to_sign).encode('utf-8'), hashlib.sha256).hexdigest()
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 datetime
import random
import string
import hashlib
import base64
import hmac
import urllib.parse
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://{{account_id}}.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}
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}"'
Note : In Oracle NetSuite Target/Operations pre-request script, users can select either the "POST" or "PATCH" method based on their requirements. Please specify the chosen method in the request_method parameter within the pre-request script.
Target / Operations for SOAP body
import os
import requests
import time
import hashlib
import hmac
import base64
import secrets
# 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 = secrets.token_hex(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
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
endpoint_url='{{hostname}}' # Provide Values
master_key = '{{master_key}}' # Provide Values
resource_type = '{{resource_type}}' # Provide Values
resource_id = '{{resource_id}}' # Provide Values
################################################################################################################################################################################
key = base64.b64decode(master_key)
endpoint_method = 'post'
now = datetime.now()
stamp = mktime(now.timetuple())
date = format_date_time(stamp)
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())
body = text.encode('utf-8')
digest = hmac.new(key, body, sha256).digest()
signature = base64.encodebytes(digest).decode('utf-8')
key_type = 'master'
version = '1.0'
uri = f'type={key_type}&ver={version}&sig={signature[:-1]}'
authorization = quote(uri)
Amazon S3
Method : PUT
Note: This pre_request_script is for loading data to Amazon S3 Bucket.
import hashlib
import hmac
import datetime
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
################################################################################################################################################################################
method = 'PUT'
amzdate = datetime.datetime.utcnow().strftime('%Y%m%dT%H%M%SZ')
datestamp = datetime.datetime.utcnow().strftime('%Y%m%d')
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'
canonical_request = method + '\\n' + canonical_uri + '\\n' + canonical_querystring + '\\n' + canonical_headers + '\\n' + signed_headers + '\\n' + hashlib.sha256(payload.encode()).hexdigest()
algorithm = 'AWS4-HMAC-SHA256'
credential_scope = datestamp + '/' + region + '/s3/aws4_request'
string_to_sign = algorithm + '\\n' + amzdate + '\\n' + credential_scope + '\\n' + hashlib.sha256(canonical_request.encode()).hexdigest()
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()
signature = hmac.new(signing_key, string_to_sign.encode(), hashlib.sha256).hexdigest()
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.
import hashlib
import hmac
import datetime
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
##################################################################################################################################################################################
method = 'GET'
service = 's3'
t = datetime.datetime.utcnow()
amzdate = t.strftime('%Y%m%dT%H%M%SZ')
datestamp = t.strftime('%Y%m%d')
canonical_querystring = ''
canonical_headers = 'host:' + host + '\n' + 'x-amz-date:' + amzdate + '\n'
signed_headers = 'host;x-amz-date'
payload_hash = hashlib.sha256(('').encode('utf-8')).hexdigest()
canonical_request = method + '\n' + canonical_uri + '\n' + canonical_querystring + '\n' + canonical_headers + '\n' + signed_headers + '\n' + payload_hash
algorithm = 'AWS4-HMAC-SHA256'
credential_scope = datestamp + '/' + region + '/'+ service + '/' + 'aws4_request'
string_to_sign = algorithm + '\n' + amzdate + '\n' + credential_scope + '\n' + hashlib.sha256(canonical_request.encode('utf-8')).hexdigest()
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()
signature = hmac.new(signing_key, (string_to_sign).encode('utf-8'), hashlib.sha256).hexdigest()
authorization_header = algorithm + ' ' + 'Credential=' + access_key + '/' + credential_scope + ', ' + 'SignedHeaders=' + signed_headers + ', ' + 'Signature=' + signature