It is also possible to set headers and cookies on the response that your Twilio Function returns. The Response
object exposes the following methods to allow you to customize what headers are sent in response to incoming requests.
This method allows you to set multiple headers in a single command. It accepts an object of key-value pairs of headers and their corresponding values. You may also set multi-value headers by making the intended header an array.
If you include the Set-Cookie
header in this object, cookies will also be set to that value in addition to any other changes. Cookies must be strings with the key and value delimited by an =
sign, such as 'Key=Value'
or as a list of values such as [‘Key=Value', ‘Agent=Smith']
.
Method Parameters
Name | Type |
---|---|
headers | Object<string, string | string[]> |
Examples
1exports.handler = (context, event, callback) => {2const response = new Twilio.Response();3response.setHeaders({4// Set a single header5'content-type': 'application/json',6// You can set a header with multiple values by providing an array7'cache-control': ['no-cache', 'private'],8// You may also optionally set cookies via the "Set-Cookie" key9'set-cookie': 'Foo=Bar',10});1112return callback(null, response);13};
1exports.handler = (context, event, callback) => {2const response = new Twilio.Response();3response.setHeaders({4// You may also set cookie attributes by including a semicolon5// (`;`) delimited list of attributes6'set-cookie': ['Foo=Bar;Max-Age=86400', 'Agent=Smith;HttpOnly;Secure'],7});89return callback(null, response);10};
This method allows you to add a single header to the response. It accepts the name of the header and its intended value.
If Response.appendHeader
is called with the name of a header that already exists, that header will be converted from a string to an array, and the provided value will be concatenated to that array of values.
Method Parameters
Name | Type | Example |
---|---|---|
key | string | 'content-type' |
value | string | string[] | 'application/json' |
Examples
1exports.handler = (context, event, callback) => {2const response = new Twilio.Response();3response4.appendHeader('content-type', 'application/json')5// You can append a multi-value header by passing a list of strings6.appendHeader('yes', ['no', 'maybe', 'so'])7// Instead of setting the header to an array, it's also valid to8// pass a comma-separated string of values9.appendHeader('cache-control', 'no-store, max-age=0');1011return callback(null, response);12};
1exports.handler = (context, event, callback) => {2const response = new Twilio.Response();3response4.appendHeader('never', 'gonna')5// Appending a header that already exists will convert that header to6// a multi-value header and concatenate the new value7.appendHeader('never', 'give')8.appendHeader('never', 'you')9.appendHeader('never', 'up');10// The header is now `'never': ['gonna', 'give', 'you', 'up']`1112return callback(null, response);13};
Name | Type | Example |
---|---|---|
key | string | 'tz' |
In the following example, the client may contain a cookie tz
and send it along with the request. Upon receiving this response from your Function, tz
will be removed from the client's cookie store and not sent with subsequent requests to your Function's domain.
1exports.handler = (context, event, callback) => {2const response = new Twilio.Response();3response.removeCookie('tz');45return callback(null, response);6};
Now that you more about how to set and modify the headers in your Function responses, let's go over some of the limitations on headers and cookies so that you don't encounter as many errors.