arangodbarangodb-foxx

ArangoDB Foxx Microservices Getting Started tutorial: what is a working URI for this example?


The tutorial here Getting started · ArangoDB v3.4.0 Documentation uses this code:

// continued
router.post('/sum', function (req, res) {
const values = req.body.values;
res.send({
    result: values.reduce(function (a, b) {
    return a + b;
    }, 0)
});
})
.body(joi.object({
values: joi.array().items(joi.number().required()).required()
}).required(), 'Values to add together.')
.response(joi.object({
result: joi.number().required()
}).required(), 'Sum of the input values.')
.summary('Add up numbers')
.description('Calculates the sum of an array of number values.');

What is an example of a URI that supplies the expected parameters (two numbers)?


Solution

  • Assuming that your server instance runs on localhost:8529 over HTTP, that the database is _system and the mount point of the Foxx service /getting-started, then the URL of the /sum endpoint is:

    http://localhost:8529/getting-started/sum

    Note that the database _system is special: It is the default, which means you don't have to specify it explicitly. The following URL is equivalent:

    http://localhost:8529/_db/_system/getting-started/sum

    Replace _system with the name of the actual database if the Foxx service is mounted in another one.

    /sum is a POST route (router.post(...)) and the expected body (content/payload of the HTTP request) is described by the joi schema: A JSON object with the attribute name values and as attribute value a number array (one or more numbers).

    With Curl, you can query the service like so:

    curl --data "{\"values\":[5,6]}" http://localhost:8529/getting-started/sum

    (The request method -X POST is inferred by Curl)

    The response is a JSON object with an attribute key result and the calculated number as attribute value:

    {"result":11}

    If you try to access the URL in a browser instead, it will be a GET request (without payload) and fail with an HTTP error: 405 Method Not Allowed