import * as http from 'http';import * as mp from 'multipart';import * as buffer from 'buffer';let w = mp.newWriter();let bytes = buffer.from("hello,中国");w.setBoundary("--ABC");let mimeHeader = { "Content-Disposition": ['form-data; name="upload_file"; filename="a.txt"'], "Content-Type": ['application/octet-stream']}w.writeBuffer(mimeHeader, bytes);w.writeField("name", "Trump");w.close();let client = http.newClient();let req : http.Request = { data: w.buffer().bytes(), headers: { "Content-Type": w.formDataContentType(), }}let resp = client.post('ip', req);console.log("response = ", resp); Copy
import * as http from 'http';import * as mp from 'multipart';import * as buffer from 'buffer';let w = mp.newWriter();let bytes = buffer.from("hello,中国");w.setBoundary("--ABC");let mimeHeader = { "Content-Disposition": ['form-data; name="upload_file"; filename="a.txt"'], "Content-Type": ['application/octet-stream']}w.writeBuffer(mimeHeader, bytes);w.writeField("name", "Trump");w.close();let client = http.newClient();let req : http.Request = { data: w.buffer().bytes(), headers: { "Content-Type": w.formDataContentType(), }}let resp = client.post('ip', req);console.log("response = ", resp);
Example