The following example function adds an Origin
HTTP header to the request
if the request doesn’t already contain this header. This header is part of cross-origin resource
sharing (CORS). This example sets the header’s value to the value in the
request’s Host
header. For more information, see Origin on
the MDN Web Docs website.
This is a viewer request function.
See this example on GitHub.
- JavaScript runtime 2.0
-
async function handler(event) {
const request = event.request;
const headers = request.headers;
const host = request.headers.host.value;
// If origin header is missing, set it equal to the host header.
if (!headers.origin)
headers.origin = {value:`https://${host}`};
return request;
}
- JavaScript runtime 1.0
-
function handler(event) {
var request = event.request;
var headers = request.headers;
var host = request.headers.host.value;
// If origin header is missing, set it equal to the host header.
if (!headers.origin)
headers.origin = {value:`https://${host}`};
return request;
}