https.request(url[, options][, callback])
- url <string> | <URL>
- options <Object> | <string> | <URL> 接受来自 http.request() 的所有 options,默认值有一些不同:
- protocol 默认值: 'https:'
- port 默认值: 443
- agent 默认值: https.globalAgent
- callback <Function>
返回: <http.ClientRequest>
发出请求到安全的 Web 服务器。
还接受来自 tls.connect() 的以下额外的 options:ca、cert、ciphers、clientCertEngine、crl、dhparam、ecdhCurve、honorCipherOrder、key、passphrase、pfx、rejectUnauthorized、secureOptions、secureProtocol、servername、sessionIdContext、highWaterMark。
options 可以是对象、字符串或 URL 对象。 如果 options 是字符串,则会自动使用 new URL() 解析。 如果是 URL 对象,则会自动转换为普通的 options 对象。
https.request() 返回 http.ClientRequest 类的实例。 ClientRequest 实例是可写流。 如果需要使用 POST 请求上传文件,则写入 ClientRequest 对象。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | export const request = async(host:string,path:string):Promise< any > = >{ return new Promise( async (resolve) = > { const https = require( 'https' ); const options = { hostname: host, port: 443 , path: path, method: 'GET' }; const req = https.request(options, (res) = > { console.log( 'statusCode:' , res.statusCode); console.log( 'headers:' , res.headers); let data = ""; res.on( 'data' , (d) = > { return data + = d; }); res.on( 'end' , (d) = > { let jsonData: any = {} try { jsonData = JSON.parse(data); }catch (e){ return resolve({code: - 1 ,msg:`${e}`}) } return resolve(jsonData) }); }); req.on( 'error' , (e) = > { console.error(e); }); req.end(); }); } |
分类:
TypeScript
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统