Reputation: 183
What is the best way to consume an API in the AdonisJS controller?
It is possible to use axios and send the data to the view?
'use strict'
const axios = require('axios')
class PostController {
index({ view }) {
const api = axios.get()...
return view.render('welcome', { name, text })
}
}
module.exports = PostController
Upvotes: 1
Views: 1971
Reputation: 2302
As @GersonLCSJunior said, there is no module for that.
Adonis (e.g. vow package) uses the superagent
library for http requests. Personally, I don't like this library.
If you're using Axios, don't forget to use await
operator.
Like:
const axios = use('axios');
const querystring = use('querystring'); // https://github.com/axios/axios#nodejs
const req = await axios.post(
'https://mywebsite/',
querystring.stringify({
message: 'hello',
})
);
console.info(req)
Upvotes: 4
Reputation: 383
AdonisJS doesn't have any built-in module to send requests, so you're free to use whatever library you want. Axios should work fine.
Upvotes: 4