How Nodejs own butt imgSecCheck Ali cloud micro-letter

Nodejs this kind of language, a line of code will be able to achieve a lot of functions, very easy. But when you want to implement a function, Baidu afternoon, do not know how to write when it is remembered using C / C ++ occasion.
The same code in Java implementation is not complicated, because Java is synchronous, can be chosen nodejs is asynchronous. nodejs used less, you do not know what are some magical function, there is no magic writing.

Requirements: download pictures from a private cloud, uploaded to the micro letter the background check.

As a result, the Internet is not written by one person can directly copy; After numerous attempts, finally using the following function successfully.

const queryWeixinCheck = async(param) => {
  return new Promise((resolve, reject)=>{
    request({
        url: 'https://api.weixin.qq.com/wxa/img_sec_check?access_token=' + param.access_token,
        method: "POST",
        formData: {
            buffer: {
                value:  request(param.url),
                options: {
                    filename: param.filename,
                    contentType: 'image/' + param.extname
                }
            },
        },
    },(error, response, body) => {
          error ? reject(error) : resolve(JSON.parse(body));
    })
  });
}

Reference document:
https://www.runoob.com/nodejs/nodejs-stream.html
https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/sec-check/security. imgSecCheck.html
following this link, have a buddy like me think, directly

curl -F [email protected] 'https://api.weixin.qq.com/wxa/img_sec_check?access_token=ACCESS_TOKEN'

After the capture request, then nodejs splicing a message also achieved, but the code is not beautiful, do not vote for him.
https://developers.weixin.qq.com/community/develop/doc/000486c0fbc558719d89a281c51800?_at=1577033510988
attachment: this is the result of capture

POST /photo/api/mangement/getAccessToken HTTP/1.1
User-Agent: curl/7.29.0
Host: 112.74.68.252:8091
Accept: */*
Content-Length: 98934
Expect: 100-continue
Content-Type: multipart/form-data; boundary=----------------------------fa0769e47e5c

HTTP/1.1 100 Continue

------------------------------fa0769e47e5c
Content-Disposition: form-data; name="media"; filename="zf.png"
Content-Type: application/octet-stream

.PNG
.
...
IHDR...b...
.....AQuq....sRGB.........gAMA..(省略一张图片的大小)....a......(6.E..PG....HTTP/1.1 404 Not Found
X-Served-By: Koa
Content-Type: text/plain; charset=utf-8
Content-Length: 9
Date: Wed, 08 Apr 2020 01:43:35 GMT
Connection: keep-alive

Not Found

Note:
nodejs download pictures

request(img_url).pipe(fs.createWriteStream(temp_file.path));

nodejs read the image

fs.readFileSync(temp_file.path)

Guess you like

Origin www.cnblogs.com/bugutian/p/12661585.html