Search code examples
javascriptnode.jsapiexpressget

Get request to Api hosted in cloudflare returns 403 error when deployed to heroku


Iam trying to use the Cowin api (https://apisetu.gov.in/public/api/cowin) to fetch available slots. I am using nodejs. When I run it on local machine it works fine but after deploying to heroku it gives the following error

2021-05-09T11:07:00.862504+00:00 app[web.1]: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2021-05-09T11:07:00.862505+00:00 app[web.1]: <HTML><HEAD><META HTTP-EQUIV="Content-Type" 
CONTENT="text/html; charset=iso-8859-1">
2021-05-09T11:07:00.862506+00:00 app[web.1]: <TITLE>ERROR: The request could not be 
satisfied</TITLE>
2021-05-09T11:07:00.862508+00:00 app[web.1]: </HEAD><BODY>
2021-05-09T11:07:00.862508+00:00 app[web.1]: <H1>403 ERROR</H1>
2021-05-09T11:07:00.862509+00:00 app[web.1]: <H2>The request could not be satisfied.</H2>
2021-05-09T11:07:00.862509+00:00 app[web.1]: <HR noshade size="1px">
2021-05-09T11:07:00.862509+00:00 app[web.1]: Request blocked.
2021-05-09T11:07:00.862510+00:00 app[web.1]: We can't connect to the server for this app or 
website at this time. There might be too much traffic or a configuration error. Try again 
later, or contact the app or website owner.
2021-05-09T11:07:00.862513+00:00 app[web.1]: <BR clear="all"> 
2021-05-09T11:07:00.862514+00:00 app[web.1]: If you provide content to customers through 
CloudFront, you can find steps to troubleshoot and help prevent this error by reviewing the 
CloudFront documentation.
2021-05-09T11:07:00.862514+00:00 app[web.1]: <BR clear="all">
2021-05-09T11:07:00.862515+00:00 app[web.1]: <HR noshade size="1px">
2021-05-09T11:07:00.862515+00:00 app[web.1]: <PRE>
2021-05-09T11:07:00.862515+00:00 app[web.1]: Generated by cloudfront (CloudFront)
2021-05-09T11:07:00.862516+00:00 app[web.1]: Request ID: CW6sc_UgM9WJOFIvpk- 
ePGq7hVbYq8FuahgqPToRueh3PuLj35Q6mg==
2021-05-09T11:07:00.862517+00:00 app[web.1]: </PRE>
2021-05-09T11:07:00.862517+00:00 app[web.1]: <ADDRESS>
2021-05-09T11:07:00.862517+00:00 app[web.1]: </ADDRESS>
2021-05-09T11:07:00.862517+00:00 app[web.1]: </BODY></HTML> hello

This is my source code

const express = require('express')
var http = require('http'),
httpProxy = require('http-proxy');
const { URL, URLSearchParams } = require('url');    
const app = express();
var proxy = httpProxy.createProxyServer({});
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var request = new XMLHttpRequest();



proxy.on('proxyReq', function(proxyReq, req, res, options) {
proxyReq.setHeader('X-Special-Proxy-Header', 'foobar');
 });

var PORT = process.env.PORT || 3006;

var cron = require('node-cron');

 const getMsg = () => {

var url_orig = 'https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/findByPin'
var url = new URL(url_orig)

var params = {pincode:'226006', date:'10/5/2021'}
url.search = new URLSearchParams(params).toString();
request.open('GET',url,false)
console.log(url)
request.send();
console.log(request.responseText,'hello')
if (request.readyState == 4 && request.status == 200){
    
    return request.responseText
}
}

 cron.schedule('* * * * *', () => {
 
  fetchApi();
  });

 function fetchApi(){

var data = getMsg()
data = data.trim()
const g = JSON.parse(data) 

 for(var centre in g){
  for(var session in g[centre][0]['sessions']){
    let response = g[centre][0]['sessions'][session]
    console.log(response.min_age_limit);
           
   }
   }
   }


 var server = http.createServer(function(req, res) {

 proxy.web(req, res, {
  target: 'https://vaccines-notify.herokuapp.com'
 });
});


server.listen(PORT)

Can anyone help me to solve this issue. The api is hoted on cloudflare.


Solution

  • Cowin public APIs will not work from data centers located outside India. The Heroku data center might be located outside India and hence you are getting this error. You can follow the steps below to check the ip address and location.

    Execute this command to get your public facing IP address (from your cloud instance)

    curl ipinfo.io 
    

    This will return your IP address, location etc...

    {
      "ip": "103.**.***.25",
      "hostname": "******",
      "city": "*****",
      "region": "******",
      "country": "IN",
      "loc": "**,**",
      "org": "*****",
      "postal": "****",
      "timezone": "Asia/Kolkata",
      "readme": "https://ipinfo.io/missingauth"
    }
    

    You can try deploying your application in a data center located in India. Most of the popular Cloud providers (such as AWS, Google, IBM Cloud, Azure etc) have data centers in India.

    I have deployed my cowin application in Google Cloud (Mumbai DC).