NodeJS - Request

有了这个模块,http请求变的超简单。

  • Get请求,包含参数
let options = {
    url: 'https://idcert.market.alicloudapi.com/idcard',
    method: 'GET', 
    qs: {
        idCard: idCardOptions.idCard,
        name: idCardOptions.name
    },
    headers: { 
        'cache-control': 'no-cache',
        Authorization: 'APPCODE ' + config.idCardKey.idCardAppCode,
        'Content-Type': 'application/json'
    },
    json: true
};
    
request(options, (error, response, body) => {
   if (!error && (response.statusCode === 200 || response.statusCode === 304)) {
        resolve(body);
    } else {
        reject(error);
    }
})
  • Post请求,包含参数
let options = {
    url: config.OCRKey.OCRHost,
    method: 'POST', 
    body: {
        image: OCROptions.image,
        configure: '{"template_id":"fe60661f-7f53-4320-b5a8-c3a19301b1b81544750942"}'
    },
    headers: { 
        'cache-control': 'no-cache',
        Authorization: 'APPCODE ' + config.OCRKey.OCRAppCode,
        'Content-Type': 'application/json'
    },
    json: true
};
    
request(options, (error, response, body) => {
   if (!error && (response.statusCode === 200 || response.statusCode === 304)) {
        resolve(body);
    } else {
        reject(error);
    }
})

猜你喜欢

转载自blog.csdn.net/seaalan/article/details/85001455