How callback_url work when calling an external API

杀马特。学长 韩版系。学妹 提交于 2020-01-06 06:48:33

问题


I am using an external API provider, after calling a fetch function, it starts doing a verification, and after the verification is done (success or failure) it is supposed to post the result to a callback_url, but it doesnt.

their API provides (along with "callback_url" parameter) a "redirect_url" parameter which is supposed to redirect you to that url once verification is done, i thought i would try it to see if it works or not. and it works fine.

Here is the fetchInfo function i call in my app.js:

let payload = {
    //your unique request reference
    "reference" :`SP_REQUEST_${Math.random()}`,
    //URL where you will receive the webhooks
    "callback_url": `${URL}/notify`,
    "redirect_url": `${URL}/`,

}
    fetchInfo : async() => {
        return fetch('API_provider_domain/api/',
        {
            method : 'post',
            headers : {
                'Accept': 'application/json',
                'Content-Type'  : 'application/json',
                'Authorization' : 'Basic ' +token
                    },
            body: JSON.stringify(payload)
            })

           .then(function(response) {
                return response.json();
           }).then(function(data) {
                console.log(data)
               return data;
           })
}

Here is how i call it in the index.js file :

const myapp = express();
const sh  = require('./app');

myapp.get(`/`, (request, response) => {
  console.log('redirecting to home')
  response.send('Helloow, /');
});
myapp.post(`/notify`, async (request, response) => {
  console.log('callback notification')
  response.send('notifications');
})

myapp.listen(PORT, () =>{ 
  console.log(`Express server currently running on port ${PORT}`)
   sh.fetchInfo()
    .then((res)=>{
      console.log("finished", res)
    })
});


After calling the fetchInfo() it returns a response specifying the request status = pending, and giving me a URL for verification , so after i open the url in chrome and i finish the verification process, it is supposed to post the result automatically to that callback_url. i mean that is how callback_urls usually work right ? But it doesn't notify me back with the request status .. Is there something wrong with how i implemented the route "/notify" ? .. THanks for the help ,

来源:https://stackoverflow.com/questions/57784650/how-callback-url-work-when-calling-an-external-api

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!