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
_13exports.handler = (context, event, callback) => {_13 const response = new Twilio.Response();_13 response.setHeaders({_13 // Set a single header_13 'content-type': 'application/json',_13 // You can set a header with multiple values by providing an array_13 'cache-control': ['no-cache', 'private'],_13 // You may also optionally set cookies via the "Set-Cookie" key_13 'set-cookie': 'Foo=Bar',_13 });_13_13 return callback(null, response);_13};
_10exports.handler = (context, event, callback) => {_10 const response = new Twilio.Response();_10 response.setHeaders({_10 // You may also set cookie attributes by including a semicolon_10 // (`;`) delimited list of attributes_10 'set-cookie': ['Foo=Bar;Max-Age=86400', 'Agent=Smith;HttpOnly;Secure'],_10 });_10_10 return 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
_12exports.handler = (context, event, callback) => {_12 const response = new Twilio.Response();_12 response_12 .appendHeader('content-type', 'application/json')_12 // You can append a multi-value header by passing a list of strings_12 .appendHeader('yes', ['no', 'maybe', 'so'])_12 // Instead of setting the header to an array, it's also valid to_12 // pass a comma-separated string of values_12 .appendHeader('cache-control', 'no-store, max-age=0');_12_12 return callback(null, response);_12};
_13exports.handler = (context, event, callback) => {_13 const response = new Twilio.Response();_13 response_13 .appendHeader('never', 'gonna')_13 // Appending a header that already exists will convert that header to_13 // a multi-value header and concatenate the new value_13 .appendHeader('never', 'give')_13 .appendHeader('never', 'you')_13 .appendHeader('never', 'up');_13 // The header is now `'never': ['gonna', 'give', 'you', 'up']`_13_13 return 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.
_10exports.handler = (context, event, callback) => {_10 const response = new Twilio.Response();_10 response.removeCookie('tz');_10_10 return callback(null, response);_10};
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.