NodeJS使用@esri/arcgis-rest-request时如何调整Fetch请求选项
解决方法
@esri/arcgis-rest-request 预留了fetchOptions配置位,所有传入该字段的参数都会直接透传给底层调用的fetch方法,可直接用来传入自定义agent配置。
方式1:单次请求生效
仅在当前调用的request请求中生效,不会影响其他请求:
import fetch from "node-fetch"; import FormData from "isomorphic-form-data"; import https from "https"; import arcgisRestRequest from "@esri/arcgis-rest-request"; arcgisRestRequest.setDefaultRequestOptions({ fetch, FormData }); // 构造自定义agent const customHttpsAgent = new https.Agent({ rejectUnauthorized: false }); arcgisRestRequest.request("https://www.arcgis.com/sharing/rest/info", { // 传入fetch配置 fetchOptions: { agent: customHttpsAgent } }) .then(response => console.log(response));
方式2:全局配置生效
所有通过@esri/arcgis-rest-request发起的请求都会自动应用该配置:
import fetch from "node-fetch"; import FormData from "isomorphic-form-data"; import https from "https"; import arcgisRestRequest from "@esri/arcgis-rest-request"; arcgisRestRequest.setDefaultRequestOptions({ fetch, FormData, // 全局配置fetch参数 fetchOptions: { agent: new https.Agent({ rejectUnauthorized: false }) } }); arcgisRestRequest.request("https://www.arcgis.com/sharing/rest/info") .then(response => console.log(response));
注意:关闭
rejectUnauthorized校验存在安全风险,仅建议在开发测试环境使用,生产环境请正确部署受信任的HTTPS证书,避免绕过证书校验逻辑。
内容的提问来源于stack exchange,提问作者Ry10p
相关产品推荐
相关产品推荐

