By ulope
Unfortunately the functions docs are very, very superficial and neither contain any information on how to access the incoming request (e.g. HTTP headers, source IP, etc.) nor on how to return anything else than text responses.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Thanks for the feedback. Here are some examples in Node.js to get you started. If you prefer other languages let us know but it’s the same model
function to perform an HTTP redirect
function main() {
return {
headers: { location: 'https://example.com' },
statusCode: 302
}
}
set a cookie and return some html
function main() {
return {
headers: {
'Set-Cookie': 'UserID=Jane; Max-Age=3600; Version=',
'Content-Type': 'text/html'
},
statusCode: 200,
body: '<html><body><h3>hello</h3></body></html>' }
}
return image/png (or other binary content, must be base64 encode
function main() {
let png = <base 64 encoded string>
return { headers: { 'Content-Type': 'image/png' },
statusCode: 200,
body: png };
}
return json (default response)
function main(params) {
return {
statusCode: 200,
headers: { 'Content-Type': 'application/json' },
body: params
};
}
The statusCode is optional and defaults to 200 OK. The headers are also optional. The body is required for a valid response. (This is the same as AWS Lambda.)
There are several properties available in the application’s context called __ow_headers, __ow_path, __ow_method which provide additional information about the calling context. These are available as arguments to the function. The first is the request headers, the second is the path segment (after your function name), and the last is the HTTP verb used to invoke the function. In addition the values __ow_query and __ow_body may be present (the latter base64 encoded) for binary and non-JSON input types. Here is an example JavaScript/Typescript repo that demonstrates how to use these.
This comment has been deleted
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.