本文为您介绍V2.0 Node.js SDK对于HTTPS请求方式的配置。
在使用V2.0 Node.js SDK进行开发时,您可以在Config中配置请求协议,当Client在调用OpenAPI时,使用指定的请求协议进行通信。建议使用HTTPS,这样可以提升数据传输的安全性。若不设置,则使用OpenAPI默认支持的请求协议类型(HTTPS):
// 配置通过 HTTPS 协议发送请求
const config = new $OpenApi.Config({
protocol: 'https',
});
重要
使用HTTPS协议访问OpenAPI时,SDK会默认开启校验SSL/TLS证书有效性,若您代码环境没有证书环境,则会报错证书校验失败。
为保障通信安全,建议您保持开启,若在测试环境必须忽略证书校验,可以通过运行时参数ignoreSSL
设置:
const runtime = new $Util.RuntimeOptions({
// true 忽略证书校验;false 设置证书校验
ignoreSSL: true,
});
TypeScript 版本:
import Ecs20140526, * as $Ecs20140526 from '@alicloud/ecs20140526';
import * as $OpenApi from '@alicloud/openapi-client';
import Credential from '@alicloud/credentials';
import Util, * as $Util from '@alicloud/tea-util';
export default class Client {
static async main(): Promise<void> {
// 默认Credential
const cred = new Credential();
const config = new $OpenApi.Config({
credential: cred,
// 访问的域名
endpoint: "ecs.cn-beijing.aliyuncs.com",
protocol: 'https',
});
const client = new Ecs20140526(config);
const request = new $Ecs20140526.ModifySecurityGroupRuleRequest({
regionId: 'cn-beijing',
securityGroupId: 'sg-2zec0dm6qi66XXXXXXXX',
securityGroupRuleId: 'sgr-2zeah9lqneb4XXXXXXXX',
policy: 'accept',
ipProtocol: 'tcp',
portRange: '5004/5004',
});
// 创建RuntimeObject实例并设置运行参数。
const runtime = new $Util.RuntimeOptions({
// true:忽略证书校验;false:设置证书校验。默认为校验。
ignoreSSL: true,
});
const resp = await client.modifySecurityGroupRuleWithOptions(request, runtime);
console.log(resp.headers);
console.log(resp.body);
}
}
Client.main();
JavaScript 版本:
const { default: Ecs20140526, ModifySecurityGroupRuleRequest } = require('@alicloud/ecs20140526');
const { Config } = require('@alicloud/openapi-client');
const { RuntimeOptions } = require('@alicloud/tea-util');
const { default: Credential } = require('@alicloud/credentials');
async function main() {
// 默认Credential
const cred = new Credential();
const config = new Config({
credential: cred,
// 访问的域名
endpoint: 'ecs.cn-beijing.aliyuncs.com',
protocol: 'https',
});
const client = new Ecs20140526(config);
const request = new ModifySecurityGroupRuleRequest({
regionId: 'cn-beijing',
securityGroupId: 'sg-2zec0dm6qi66XXXXXXXX',
securityGroupRuleId: 'sgr-2zeah9lqneb4XXXXXXXX',
policy: 'accept',
ipProtocol: 'tcp',
portRange: '5004/5004',
});
// 创建RuntimeObject实例并设置运行参数。
const runtime = new RuntimeOptions({
// true:忽略证书校验;false:设置证书校验。默认为校验。
ignoreSSL: true,
});
const resp = await client.modifySecurityGroupRuleWithOptions(request, runtime);
console.log(resp.headers);
console.log(resp.body);
}
main();