Home > Software design >  How to get result from third-party api
How to get result from third-party api

Time:03-15

I want to use a third-party api: Latin WordNet API, but I meet some problems.

  1. The api document shows how to get result by url in browser, but I don't know how to get result by other way.
  2. I try to use axios through HTML script element to get the result, like:
const btn = document.querySelector('#searchBtn')

btn.addEventListener('click', function () {
    const options = {
        method: 'GET',
        url: 'https://latinwordnet.exeter.ac.uk/api/lemmas/virtus/n/'
    }

    axios(options).then(data => console.log(data))  
})

But I get error about CORS. If I use proxy like ngrok, it still doesn't work.

3. I want to try it like normal route, like:

const express = require('express')
const router = express.Router()

router.get('/api/lemmas/virtus/n/', (req, res) => {
    console.log(res)
})

I don't know where the result will come from, and I'm also not sure this way is right or false.

Hope anyone may give some tips.

CodePudding user response:

try with GET request:

 const options = {
        method: 'GET',
        url: 'https://latinwordnet.exeter.ac.uk/api/lemmas/virtus/n/'
    };

    axios(options)
    .then(response => console.log(response.data.results))
    .catch(err=>console.log(err.response.data));

CodePudding user response:

I found a way to deal with it:

const express = require('express')
const axios = require('axios')
const router = express.Router()

router.get('/search', (req, res) => {
    axios.get(`https://latinwordnet.exeter.ac.uk/api/lemmas/${req.query.word}/n/`)
         .then(response => {
            return res.json(response.data.results)
         })
})

combine route and axios

  • Related