文档首页/ Astro轻应用 AstroZero/ 常见问题/ 应用后端开发/ AstroZero脚本中是否支持multipart/form-data请求?
更新时间:2024-06-19 GMT+08:00
分享

AstroZero脚本中是否支持multipart/form-data请求?

脚本标准库提供了“multipart”,支持multipart/form-data请求, 具体示例代码如下:

import * as http from 'http';
import * as mp from 'multipart';
import * as buffer from 'buffer';

let w = mp.newWriter();

let bytes = buffer.from("hello, Astro Zero");

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);

相关文档