Postman data encryption and decryption to implement APP login interface simulation request

Postman data encryption and decryption to implement APP login interface simulation request

Mainly used Postman functions

  • Environment variables: Just create a new one, and all operations are handled in the code.
  • Log view: Menu location: View → show postman console, just display this window view
  • Script executed during request: Pre-request Script tag, using language javascript, usually as encryption.
  • Accepts the script to be executed when returning: Tests tab, as the processing of the return parameter, usually decryption.

Simple interface

Data encryption and decryption

The interface requires that the params data be encrypted and sent in JSON format.

Various parameter settings

  • Fill in the plain text parameters:

The parameters here are the basic data used by the script later, which can also be similar to the data that needs to be filled in the APP login interface.

Interface parameters

  • Fill in the request header parameters:

The request header data here are some established rule parameters agreed upon by the system interface.

Request header parameters

The data actually sent:

Yes, there is only one parameter. After the script processes and encrypts the parameter, it becomes a long string ╮(╯_╰)╭

body data.

Pro-request Script

To process the script, just look at the code.

Some commonly used encapsulation classes of scripts are encapsulated by postman, such as:

  • Environment variable: pm.environment
  • Data encryption toolkit: CryptoJS
  • Mathematical function toolkit: Math

[Next to the code writing box, there is a common operation code generation provided by Postman, which is very useful]

Specific code:

// Set environment variable parameters for other requests pm.environment.set("lkm-sys-id",pm.request.headers.get("lkm-sys-id"));
pm.environment.set("lkm-app-id",pm.request.headers.get("lkm-app-id"));
pm.environment.set("lkm-organ-id",pm.request.headers.get("lkm-organ-id"));
pm.environment.set("lkm-app-ver",pm.request.headers.get("lkm-app-ver"));

// JSON parameters for data signature const paramsString = request.url.split('?')[1]; 
const eachParamArray = paramsString.split('&'); 
let params = {}; 
eachParamArray.forEach((param) => { 
    const key = param.split('=')[0]; 
    const value = param.split('=')[1]; 
    Object.assign(params, {[key]: value}); 
}); 
console.log(params);

//Data processing var requestParams = params;
console.log(requestParams);

// Data signature processing defined by the interface var timestamp = Math.round(new Date());
pm.environment.set("timestamp",timestamp);
var shuijishu = "XO337hNxWyNsOlDw";
pm.environment.set("shuijishu",shuijishu);
// Data signature, agreed upon by the system interface var lkmSign = pm.request.headers.get("lkm-sys-id")
                + pm.request.headers.get("lkm-app-id")
                + "1122334455667788"
                + timestamp
                + shuijishu
                + ""
                ;
pm.environment.set("lkmSign",CryptoJS.MD5(lkmSign).toString().toUpperCase());
// ================== Data signature processing ends=====================================

// ===================== Encryption and decryption data============================================
/// aesdata processing var aesData = dealWithAesData(requestParams);
pm.environment.set("data", aesData); // This is the data needed to be sent in the screenshot above./// ↓↓↓↓↓↓↓↓↓↓↓↓↓ Commonly used AES encryption and decryption methods↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ ///
// aesData data processing (content dictionary)
function dealWithAesData(content) {
    console.log('aesData plain text: ' + JSON.stringify(content));
    const key = CryptoJS.enc.Utf8.parse("****************");
    const iv = CryptoJS.enc.Utf8.parse("0000000000000000");
    const encrypted = CryptoJS.AES.encrypt(JSON.stringify(content), key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); //CBC
    return encrypted.toString();
}

Response processing script [Tests]

  • Response data, processing is decryption and display.
  • Note: Postman provides various assertion operations, which can display the test results well.

Specific code:

// Return data processing var jsonData = JSON.parse(responseBody);
var responseData = JSON.parse(decryptResponseData(jsonData.data)); // Data decryption let success = responseData['success'];
var responseResult = responseData["data"];

//Data token record, used by subsequent interfaces try {
    let token = responseResult["token"];
    pm.environment.set("token", "Bearer "+token);
    console.log("token: " + token);
} catch (err) {
    tests['Expect response data to be valid token'] = false;
    tests[`Response data: ${responseResult}`] = true;
    console.error(err);
}

//Data log printing console.log("jsonData: The following object is the response data")
console.log(jsonData);
console.log("responseData: The following object is the first layer of parsed data")
console.log(responseData);
console.log("responseResult: The following object is the second layer of parsed data")
console.log(responseResult);

// Environment variable settings, used by other interfaces after logging in.
console.log(responseResult.agentId);
pm.environment.set("agentId",responseResult.agentId);
pm.environment.set("agentCode",responseResult.agentCode);
pm.environment.set("agentName",responseResult.agentName);
// pm.environment.set("token",responseResult.token);

// Set assertion Postman provides a good general assertion generation, you can try tests["Business return code=200, success!"] = responseData.code === 200;

/// Decrypt the returned data function decryptResponseData(content) {
    const key = CryptoJS.enc.Utf8.parse("*****************");
    const iv = CryptoJS.enc.Utf8.parse("0000000000000000");
    const decrypt = CryptoJS.AES.decrypt(content, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
    const decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
    return decryptedStr.toString();
}

The result looks like

Final data

The request for simulating the encrypted interface is almost like this. As for other operations, you will have to learn them later. For more information about postman login APP interface requests, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • How to set parameter list when requesting POST in postman
  • Example of sending a POST request in JSON format using POSTMAN
  • Installation and use of postman (simulating Get and Post requests)
  • Postman simulates sending a request method with a token
  • Postman test post request parameter is json type example explanation
  • PostMan post request method to send Json data
  • Postman simulates four request bodies of post request

<<:  Detailed explanation of how to configure Nginx web server sample code

>>:  Detailed tutorial on how to compile and install mysql8.0.29 in CentOS8 deployment LNMP environment

Recommend

Detailed explanation of Mysql's concurrent parameter adjustment

Table of contents Query cache optimization Overvi...

Detailed usage of Vue timer

This article example shares the specific code of ...

Disable IE Image Toolbar

I just tried it on IE6, and it does show the toolb...

How does MySQL connect to the corresponding client process?

question For a given MySQL connection, how can we...

Docker Gitlab+Jenkins+Harbor builds a persistent platform operation

CI/CD Overview CI workflow design Git code versio...

Mysql 5.6 "implicit conversion" causes index failure and inaccurate data

background When performing a SQL query, I tried t...

Detailed process analysis of docker deployment of snail cinema system

Environmental Statement Host OS: Cetnos7.9 Minimu...

How to use JSZip compression in CocosCreator

CocosCreator version: 2.4.2 Practical project app...

Implementation steps for building FastDFS file server in Linux

Table of contents 1. Software Package 2. Install ...

Vue's guide to pitfalls using throttling functions

Preface In a common business scenario, we need to...

Self-study of MySql built-in functions knowledge points summary

String functions Check the ascii code value of th...

Native JavaScript message board

This article shares the specific code of JavaScri...

Detailed explanation of nginx configuration file interpretation

The nginx configuration file is mainly divided in...