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
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.
As @GersonLCSJunior said, there is no module for that.
Adonis (eg 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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.