request.setHeader(name, value)


为标头对象设置单个标头值。 如果该标头已经存在于待发送的标头中,则其值将被替换。 在此处使用字符串数组发送具有相同名称的多个标头。 非字符串值将不加修改地存储。 因此,request.getHeader() 可能返回非字符串值。 但是,非字符串值将转换为字符串以进行网络传输。

request.setHeader('Content-Type', 'application/json');

或者

request.setHeader('Cookie', ['type=ninja', 'language=javascript']);

当值为字符串时,如果它包含 latin1 编码之外的字符,则会抛出异常。

如果您需要在值中传递 UTF-8 字符,则使用 RFC 8187 标准对值进行编码。

const filename = 'Rock 🎵.txt';
request.setHeader('Content-Disposition', `attachment; filename*=utf-8''${encodeURIComponent(filename)}`);

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

When the value is a string an exception will be thrown if it contains characters outside the latin1 encoding.

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