Question

Functions: how to access request info / return other response types

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.

Show comments

Submit an answer
Answer a question...

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!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Rodric Rabbah
DigitalOcean Employee
DigitalOcean Employee badge
May 24, 2022

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.

Rodric Rabbah
DigitalOcean Employee
DigitalOcean Employee badge
May 24, 2022

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.)

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Rodric Rabbah
DigitalOcean Employee
DigitalOcean Employee badge
June 3, 2022

This comment has been deleted