Sending Outgoing HTTP responses

Interacting with outgoing HTTP responses from a Napkin function follows the same pattern as that of [requests]. For Python, response data is set via the built-in napkin.response Python module. For Node.js, you must export a default function that takes two parameters, req (request) and res (response). This was intentionally designed to mimic the Express API, which most Node.js developers are already familiar with.

Python

from napkin import response

Node.js

export default (req, res) => {
    // ...
}

Setting Headers

You can set outgoing HTTP headers by using the headers field on napkin.response (for Python) or the res parameter (for Node.js). With Node.js, you can also use the response object's append(key, value) and set(key, value) functions to set outgoing headers. Multiple options are provided to better mimic the [Express API}(https://expressjs.com/en/api.html#res).

Python

from napkin import response

response.headers['my-header'] = 'hello'

Node.js

export default (res, req) => {
    // two ways to set headers in JS
    res.headers["my-header"] = "hello"
    // or
    res.append("my-header", "hello")
}

Response Payload

The way you send response data depends on the runtime you're using. For Python, you can send back data by setting the napkin.response.body attribute. For Node.js, you may use either the res.send method or the res.json method.

Python

napkin.response.body can be set to either a str type or a dict. If the value of napkin.response.body is a dict, Napkin will automatically configure the outgoing response to include the Content-Type: application/json header. If the value of napkin.response.body is of type str, Napkin sets the Content-Type header to text/html. You can override these behaviors by setting the Content-Type header yourself.

from napkin import response

response.body = "Just some string"
# or
response.body = {
    "message": "this is a string inside a dictionary"
}

Javascript

res.send

Use res.send when you just want to send back text in the request body. Napkin will automatically set the Content-Type header to text/html when res.send is used, unless you set the Content-Type header yourself.

export default (req, res) => {
    res.send("This is just some text")
}
res.json

Use res.json to send data in the response as a JSON object. When res.json is used, Napkin will automatically set the response's Content-Type header to application/json, unless you set the Content-Type header yourself.

export default (req, res) => {
    res.json({
        myMessage: "Eat your vegetables!"
    })
}

HTTP Status Codes

You can set the response status code to indicate whether or not your function successfully processed the request. By default, the status code is set to 200 unless the function fails execution, in which case the status code is set to 400.

Python

from napkin import response

response.status_code = 204

Node.js

export default (req, res) => {
    res.status(204)
}

CORS

Coming soon.