Api Rest, get method, how to pass array parameters

1

I have developed a restfull api in Node.js, the client is developed in angular, how can I send array parameters in a get, should it be sent by the header or by the parameters ?. I currently send it in the body of a post but I do not want to use this method to obtain information.

    
asked by Ezio 29.08.2018 в 22:29
source

1 answer

0

Depends on what you are using to build your API. In the case of express (and loopback), a route of the type:

app.get('/', (req, res, next) => {

});

You can capture the query string parameters using req.query . For example:

app.get('/', (req, res, next) => {
   console.log(req.query);
});

If you call a URL of the type

http://localhost:3000/?a=1&a=2&a=3

The console logs:

{ a: [ '1', '2', '3' ] }

If you are using another framework, the important thing is that you have access to the variable that contains the complete URL of the request, and use the module url that is built into node. Let's say:

    var address = 'http://localhost:3000/?a=1&a=2&a=3';

    var url = require('url');
    var url_parts = url.parse(address, true);

    console.log(url_parts.query);

And that prints the same as the example above

    
answered by 30.08.2018 в 00:52