export const requestFormData = async(host:string,path:string,formData:any):Promise<any> =>{ return new Promise( async (resolve) => { const https = require('https'); const options = { hostname: host, path: path, method: 'POST', headers:formData.getHeaders() }; 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); }); formData.pipe(req); //req.end(); }); } export const requestFile = async(host:string,path):Promise<any> =>{ return new Promise( async (resolve) => { const FormData =require('form-data'); const formData = new FormData(); const filename="./test.txt" // 1.先将字符串转换成Buffer const fileContent = Buffer.from("hello"); // 2.补上文件meta信息 formData.append(filename, fileContent, { filename, contentType: 'text/plain', knownLength: fileContent.byteLength }); //const fs = require("fs"); //formData.append("template",fs.createReadStream('./test.txt')) let response = await requestFormData(host, path,formData) return resolve(response); }); }