cowinapi / developer.cowin

This group is created to facilitate technical and integration discussions related to cowin platform. API related contents can be obtained at API setu portal https://apisetu.gov.in/public/marketplace/api/cowin
115 stars 30 forks source link

GET request to "https://cdn-api.co-vin.in/api/v2/admin/location/states" not working in heroku but working in localhost. #530

Open sasikanthtadimalla opened 2 years ago

sasikanthtadimalla commented 2 years ago

I am using NodeJS and axios.

I am simply making a GET request to this API: https://cdn-api.co-vin.in/api/v2/admin/location/states

I am getting the data properly in localhost, postman, as well as simple browser request.

When I make the same request using axios in nodeJS, it works on localhost. But if I deploy it to heroku and then make the request, it fails with the error 403.

Why is this happening?

Here's the code:

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

router.get('/getStates', (req, res) => {
  const config = {
    method: 'get',
    url: `https://cdn-api.co-vin.in/api/v2/admin/location/states`,
    headers: {}
  };

  axios(config)
  .then((response) => {
    res.json({data: response.data.states});
  })
  .catch((error) => {
    res.json({message: 'There is an error'});
  });
})

module.exports = router;

Here's the error:

"error": {
        "message": "Request failed with status code 403",
        "name": "Error",
        "stack": "Error: Request failed with status code 403\n    at createError (/app/node_modules/axios/lib/core/createError.js:16:15)\n    at settle (/app/node_modules/axios/lib/core/settle.js:17:12)\n    at IncomingMessage.handleStreamEnd (/app/node_modules/axios/lib/adapters/http.js:260:11)\n    at IncomingMessage.emit (events.js:412:35)\n    at endReadableNT (internal/streams/readable.js:1317:12)\n    at processTicksAndRejections (internal/process/task_queues.js:82:21)",
        "config": {
            "url": "https://cdn-api.co-vin.in/api/v2/admin/location/states",
            "method": "get",
            "headers": {
                "Accept": "application/json, text/plain, */*",
                "User-Agent": "axios/0.21.1"
            },
            "transformRequest": [
                null
            ],
            "transformResponse": [
                null
            ],
            "timeout": 0,
            "xsrfCookieName": "XSRF-TOKEN",
            "xsrfHeaderName": "X-XSRF-TOKEN",
            "maxContentLength": -1,
            "maxBodyLength": -1
        }
    }

I again repeat, I am facing this only while making the request from heroku. It is working well on localhost.

saran-surya commented 2 years ago

Hi @sasikanthtadimalla , It will not work on Heroku because it is restricted to Indian IP, There is a workaround, But you will have to set up the proxy for the server using this package. I am currently using it for accessing the data from Heroku, And it does work, but you will need a system running in India for the proxy server, If not you can try getting AWS Mumbai or google cloud's Bangalore server.

https://github.com/saran-surya/cassata