I have the following python example which I need a JavaScript equivalent as a pre-request script for postman so I can test access to a 3rd party API, If anyone could assist me I would really appreciate it.
#!/usr/bin/python3
import requests
import urllib3
import base64
import hashlib
import hmac
import time
from urllib import parse
import json
url = 'https://api.xxxxxxxxxx.com'
api_key = 'yourApiKey'
api_secret = 'yourApiSecret'
def request(method, path, headers=None, payload=None, query=None):
timestamp = int(time.time() * 1000)
message = '\n'.join([str(e) for e in [method, path,
(parse.urlencode(query) if query else None),
(json.dumps(payload) if payload else None),
timestamp] if e != None])
hmac_hash = base64.b64encode(hmac.new(api_secret.encode(), message.encode(),
hashlib.sha512).digest()).decode()
if headers is None:
headers = {}
headers['X-API-Auth'] = '{}:{}'.format(api_key, hmac_hash)
headers['X-API-Timestamp'] = str(timestamp)
return requests.request(method, url + path, headers=headers,
params=query, json=payload)
if __name__ == '__main__':
print('Getting devices')
req = request('GET', '/v1/devices')
print(req.json())
device_id = req.json()[0]['deviceId']
print('Setting a property on a device')
req = request('PUT', '/v1/devices/' + device_id + '/properties/prop_1',
payload={'payload': '33'})
print(req.json())
I tried executing the python example which worked as expected , but really need the JavaScript equivalent so I can work within Postman
New contributor
Jason Ellwood is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.