request.setHeader(name, value)
为 headers 对象设置单个头部值。如果该头部已存在于即将发送的头部中,其值将被替换。此处使用字符串数组可以发送具有相同名称的多个头部。非字符串值将按原样存储。因此,request.getHeader() 可能会返回非字符串值。然而,非字符串值在网络传输时将被转换为字符串。
【Sets a single header value for headers object. If this header already exists in
the to-be-sent headers, its value will be replaced. Use an array of strings
here to send multiple headers with the same name. Non-string values will be
stored without modification. Therefore, request.getHeader() may return
non-string values. However, the non-string values will be converted to strings
for network transmission.】
request.setHeader('Content-Type', 'application/json'); 或者
【or】
request.setHeader('Cookie', ['type=ninja', 'language=javascript']); 当值是字符串时,如果它包含 latin1 编码之外的字符,将会抛出异常。
【When the value is a string an exception will be thrown if it contains
characters outside the latin1 encoding.】
如果你需要在值中传递 UTF-8 字符,请使用 RFC 8187 标准对该值进行编码。
【If you need to pass UTF-8 characters in the value please encode the value using the RFC 8187 standard.】
const filename = 'Rock 🎵.txt';
request.setHeader('Content-Disposition', `attachment; filename*=utf-8''${encodeURIComponent(filename)}`);