I have a functional requirement to POST a URL and pass some parameters. The app_id and app_key need to be calculated and passed as parameters. The content parameter is passed in from the previous node. The Python code implementation is as follows.
How to implement this function in an n8n node? I don’t have a Python environment, and the code node can only execute JavaScript. The HTTP Request node seems unable to perform operations. It is best not to use hard coded app_id and app_key.
I hope someone can provide useful suggestions to implement this feature, or offer other simple and easy-to-use translation methods.
Hi @blckder02
You don’t need Python for this.
Use a Code node in JavaScript to calculate the app_id and app_key dynamically (doing the same math and hashing your Python code would do) and return those values.
Then use an HTTP Request node to send the POST. In that node, reference the values from the Code node and the content from the previous node using expressions, instead of hard coding anything.
In short: calculate everything in a Code node, send everything in an HTTP Request node.
The Python code is as follows. How to achieve without hard coding app_id and app_key, and how to obtain app_id and app_key in the code node
app_id = app_id
app_key = app_key
salt = random.randint(32768, 65536)
md5 = hashlib.md5()
sign_str = app_id + content + str(salt) + app_key
md5.update(sign_str.encode())
sign = md5.hexdigest()
url = “https://xxxx.com/api/trans/”
param = {
“q”: content,
“from”: “en”,
“to”: “zh”,
“appid”: app_id,
“salt”: salt,
“sign”: sign
}
data = urllib.parse.urlencode(param).encode(‘utf-8’)
with urllib.request.urlopen(url, data=data) as response:
“-----Processing response data-----”