Below is my Pre Request Script working Fine
const CryptoJS = require('crypto-js');
// Function to encrypt data
function encryptData(plainData, key, iv) {
const encrypted = CryptoJS.AES.encrypt(plainData, CryptoJS.enc.Utf8.parse(key), {
iv: CryptoJS.enc.Utf8.parse(iv),
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7,
});
return encrypted.toString();
}
// Encryption key and IV (use environment variables for security)
const key = pm.environment.get('encryptionKey') || '8264754987545454'; // Replace with your key
const iv = pm.environment.get('encryptionIV') || '764851343443456184'; // Replace with your IV
// Raw request body
const requestBody = {
"CustCode": "H0000972"
}
;
// Encrypt the request payload
const encryptedPayload = encryptData(JSON.stringify(requestBody), key, iv);
// Prepare the final payload to include the encrypted content and IV
const finalPayload = {
content: encryptedPayload,
iv: CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(iv)),
};
// Set the encrypted payload as the request body
pm.request.body.raw = JSON.stringify(finalPayload);
// Log the encrypted payload for debugging
console.log('Encrypted Payload:', finalPayload);
After Getting actual response on json body response
{\"content\":\"DZBjHydiOa8LvGYUPOqxVF4Qf7UdclpzTV8ih2WGsrIat8d1FHSLJ4bRaXqtJ20v+67XxtH9kuzAxVWhvG3U3tPWbmtJZxUh3VU=\"}"
Error in response on console
Invalid response structure or missing “content” field:
{"content":"DZBjHyd+jKRK422hX8YGJSkK4/gvmETwjyoeYjj+jrDfasY6/1CqCuixGQ21+dP1oH4aoU8dPApXVZXax3yucLw+LDJVgyEZ7tenBboMwAc3kGGdJxvatkXnge94YnxTdOblzVdaiDzXm5m1tseFn2URZGV6lwQaZCoQUIAByZ2CoQpY6cpFwvezwo9utbkv9vyzqsHzKKfLKKPomN7Cgi6q1merAZ8nMV9M/8akGqL1AeabmtJZxUh3VU="}
Error: Invalid response structure or missing "content" field.
Error getting
console response : {"content":" response \"}"
Json Body Response :- "{\"content\":\" body response \"}" correct