background

When I was in Shanghai, I always felt it was time for the nucleic acid to be released, but every time I opened the health cloud/application, the result was “waiting for the test institution to upload”. My friend in Pudong and I took 0.5 days to 2 days respectively to get the results. The waiting time was “green as a fruit”.

If only it would refresh the results automatically, instead of every time I – open my phone – find wechat/Alipay – open health Cloud/apply – click to see the results.

Train of thought

When THE author was engaged in the development of MOBILE TERMINAL H5 earlier, Charles environment was configured. That is to say, after MY phone was connected to the Charles of the computer, I could see the requests and returns of the interface in every web page opened by my phone.

Based on this, I can see the URL of the interface, request parameters (including headers and body, my own personalization parameters), and write a NodeJS script to poll it.

Start.

Implementation steps

1. Charles on your phone

The phone is connected to the computer’s Charles capture bag, here is a brief introduction, not detailed explanation, there are many tutorials on the Internet.

1. Install and open Charles on the computer. After installing the computer system certificate, click as shown in the picture:





2. Connect the mobile phone agent to Charles, set the server and port of the agent according to the configuration as indicated in (1), and then open the browser to install the corresponding certificate and trust:

2. Open wechat – Health Cloud mini program – nucleic acid test results, Charles can see the request and return of the interface.

Of course, some people asked whether it was ok to apply with Alipay, but Charles didn’t catch it. When entering the application result page, there were more than a dozen requests. Maybe I missed it.

The health cloud should have only one or two, and it will soon find that interface.

Three, realize the interface request

1. If you know some NodeJS, you can write code to initiate a request based on the above URL and request parameters (headers, body, etc.).

I won’t go into details about how to do this, because I’ll put the full sample code at the end.

2, with the help of tools:

$cUrl = $Charles = $Charles = $Charles = $Charles = $Charles = $Charles = $Charles = $Charles = $Charles = $Charles = $Charles = $Charles Finally, extract nodeJS from Postman (for those of you who are not good at nodeJS request interface/who are too lazy to determine headers parameters).

CUrl confirms whether the request is successful

Select this interface in Charles and right-click Copy cURL Request to Copy cURL commands to the clipboard

Then open any command window, paste directly, and press Enter: to see that the result is returned.

CUrl: cUrl: cUrl: cUrl: cUrl: cUrl: cUrl: cUrl: cUrl: cUrl: cUrl How do I do that? I think of Postman.

(2) cURL command, borrow postman, get nodeJS code.

Open the Postman application, File – Import in the upper left corner, select Raw Text, paste in the cCURL command, and click Continue – Import.

Now that the postman link has requested headers and body, click “Send” to see it return:

Next, to get the nodeJS code from Postman, just click </>, which is the code, and select the nodejs-request you’re familiar with:

Copy the complete NodeJS code.

4. Complete nodeJS code (add polling) and run it.

To create a file called lunxun.js, paste the nodejs extracted from Postman as follows:

var request = require('request');
var options = {
  'method': 'POST'.'url': 'https://h5.wdjky.com/gw/nad-biz-register-queryReportEncrypted'.'headers': {
    'Host': 'h5.wdjky.com'.'Accept': 'text/plain, */*; Q = 0.01 '.'timestamp': '1649245676026'.'X-Requested-With': 'XMLHttpRequest'.'Accept-Language': 'zh-CN,zh-Hans; Q = 0.9 '.'token': 'xxxx'.// change XXXX to your own
    'lightAppCode': '10000'.'Origin': 'https://h5.wdjky.com'.'channelCode': '10039'.// 'user-agent ':' XXXX ', // non-required field (XXXX)
    // 'Referer': 'Referer', // 'Referer': 'Referer', //
    'Content-Type': 'application/json; charset=utf-8'.// 'request-id': 'XXXX ', //
  },
  body: '{"accountId":"xxxx","publicKey":"xxxx"}'  // change XXXX to your own

};
request(options, function (error, response) {
  if (error) throw new Error(error);
  console.log(response.body);
});

Copy the code

Minor improvements, such as adding polling, result in:

// Note that the main changes are as follows:
var request = require('request');
var options = {
  'method': 'POST'.'url': 'https://h5.wdjky.com/gw/nad-biz-register-queryReportEncrypted'.'headers': {
    'Host': 'h5.wdjky.com'.'Accept': 'text/plain, */*; Q = 0.01 '.'timestamp': '1649245676026'.'X-Requested-With': 'XMLHttpRequest'.'Accept-Language': 'zh-CN,zh-Hans; Q = 0.9 '.'token': 'xxxx'.// change XXXX to your own
    'lightAppCode': '10000'.'Origin': 'https://h5.wdjky.com'.'channelCode': '10039'.// 'user-agent ':' XXXX ', // non-required field (XXXX)
    // 'Referer': 'Referer', // 'Referer': 'Referer', //
    'Content-Type': 'application/json; charset=utf-8'.// 'request-id': 'XXXX ', //
  },
  body: '{"accountId":"xxxx","publicKey":"xxxx"}'  // change XXXX to your own

};

// Query:
function update() {
    request(options, function (error, response) {
        if (error) return console.error(error);
        const body = JSON.parse(response.body)
        const natResult =  body.data[0].natResult
        console.log(natResult);
    });
}

setInterval(() = > {
    update()
}, 2000)

Copy the code

Using the tool, I found that 30 of the 36 lines of code had been written by the tool.

The last is to run it:

yarn add request && node lunxun.js

PNPM add Request && Node lunxun.js if you prefer PNPM.

Five, to see the effect

The effect of repeated requests:

The results appear:

summary

This article introduces, if let nucleic acid test results automatically refresh for ourselves to see. Mainly with the help of Charles tool to capture the health cloud result page request and return, and then turned into nodeJS script to send requests (parameters have, familiar with nodeJS students can actually write directly. Using cURL and Postman to get the nodeJS code to initiate an interface request, you can also add the polling part.