本文為您介紹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();