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

unexpected token < in json when calling the api from server, working fine with localhost #232

Closed abhinavvsinhaa closed 3 years ago

abhinavvsinhaa commented 3 years ago

whenever i am trying to call the api from my server, its showing this error undefined:1 unexpected token < in json at position 0, while it is working completely fine when I am calling it from my local host. please help me

kumaraswins commented 3 years ago

which framework do you use? flask? django or node? check you have opened appropriate ports in the server

abhinavvsinhaa commented 3 years ago

I'm using node and hosting it on Digital Ocean, don't know why it's not working in there, but absolutely smooth in localhost. Can you tell me what request do we need to make to the cowin api, so that I can cross check mine

abhinavvsinhaa commented 3 years ago
let options = {
                    'method': 'GET',
                    'url': `https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByPin?pincode=${row.pincode}&date=${day}`,
                    'headers': {

                    }

                };
request(options, function (error, re) {
                    if (error) {
                        console.log('error in request',error)
                    }
                    else if(re) {
                        let data = JSON.parse(re.body);
                        console.log(data)
}
})

@kumaraswins 2021-05-17

kumaraswins commented 3 years ago

These API are geo fenced. It works only with Indian network . So make sure your digital ocean is hosted in Mumbai or Indian region.

abhinavvsinhaa commented 3 years ago

it's in Bengaluru. Do you find any error within my code? Sometime it works fine on the server sometimes it doesn't with the same code.

kumaraswins commented 3 years ago
var request = require('request');
var options = {
  'method': 'GET',
  'url': 'https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/findByPin?pincode=110001&date=17/05/2021',
  'headers': {
    'accept': 'application/json',
    'Accept-Language': 'hi_IN'
  }
};
request(options, function (error, response) {
  if (error) throw new Error(error);
  console.log(response.body);
});

Make sure it is in this format

abhinavvsinhaa commented 3 years ago

Don't need to parse it with JSON.parse?

abhinavvsinhaa commented 3 years ago

ya you were right, the time is showing Mon May 17 03.54 UTC 2021

kumaraswins commented 3 years ago

issue sorted?

saran-surya commented 3 years ago

Hello @abhinav-sinhaa , why are you using Requests while they are already depricated(On maintenance), And try using some other alternatives, such as AXIOS

saran-surya commented 3 years ago

Important Notice,

If so you want to access a geo-fenced public API, I have a workaround for this, You can the package CASSATA to proxy your server to your current location, I have tested this on HEROKU with an US server for this API. For now it only supports get requests, and no post requests are allowed. https://github.com/saran-surya/cassata

abhinavvsinhaa commented 3 years ago

issue sorted?

No it's not sorted, since the api is geo-fenced, the server becomes unable to fetch the api, may be the issue is rising because of the server's internet connection as it is working completely fine in my local computer.

abhinavvsinhaa commented 3 years ago

Important Notice,

If so you want to access a geo-fenced public API, I have a workaround for this, You can the package CASSATA to proxy your server to your current location, I have tested this on HEROKU with an US server for this API. For now it only supports get requests, and no post requests are allowed. https://github.com/saran-surya/cassata

Will try this today, and let you know if this works

saran-surya commented 3 years ago

Sure @abhinav-sinhaa, I currently have this working for my Node JS app in production, A Heroku Server in US. They are working fine. For now you can only do GET Requests, Kindly let me know if you face any issues. 🦄

abhinavvsinhaa commented 3 years ago

Sure @abhinav-sinhaa, I currently have this working for my Node JS app in production, A Heroku Server in US. They are working fine. For now you can only do GET Requests, Kindly let me know if you face any issues. 🦄

It's showing proxy server is not connected!

saran-surya commented 3 years ago

Sure @abhinav-sinhaa, I currently have this working for my Node JS app in production, A Heroku Server in US. They are working fine. For now you can only do GET Requests, Kindly let me know if you face any issues. 🦄

It's showing proxy server is not connected!

You have to connect the client, only then the PROXY will be connected, if you are calling the functions right away, You have to add a buffer time to get you enough time to connect it, It is basically doing the small scale process of proxy servers, What it will do is it will use your IP to get the data that is restricted to a area, Also you have to persist the session to maintain contact, This is a small scale version of proxy servers as said,

Kindly refer this section https://github.com/saran-surya/cassata#accessing-the-proxy-client-, You have to connect the proxy client and then access the data. This should help.

abhinavvsinhaa commented 3 years ago

Sure @abhinav-sinhaa, I currently have this working for my Node JS app in production, A Heroku Server in US. They are working fine. For now you can only do GET Requests, Kindly let me know if you face any issues. 🦄

It's showing proxy server is not connected!

You have to connect the client, only then the PROXY will be connected, if you are calling the functions right away, You have to add a buffer time to get you enough time to connect it, It is basically doing the small scale process of proxy servers, What it will do is it will use your IP to get the data that is restricted to a area, Also you have to persist the session to maintain contact, This is a small scale version of proxy servers as said,

Kindly refer this section https://github.com/saran-surya/cassata#accessing-the-proxy-client-, You have to connect the proxy client and then access the data. This should help.

Working fine on localhost, will try it now on server! Any custom specification need to be defined on server or not?

saran-surya commented 3 years ago

Sure @abhinav-sinhaa, I currently have this working for my Node JS app in production, A Heroku Server in US. They are working fine. For now you can only do GET Requests, Kindly let me know if you face any issues. 🦄

It's showing proxy server is not connected!

You have to connect the client, only then the PROXY will be connected, if you are calling the functions right away, You have to add a buffer time to get you enough time to connect it, It is basically doing the small scale process of proxy servers, What it will do is it will use your IP to get the data that is restricted to a area, Also you have to persist the session to maintain contact, This is a small scale version of proxy servers as said, Kindly refer this section https://github.com/saran-surya/cassata#accessing-the-proxy-client-, You have to connect the proxy client and then access the data. This should help.

Working fine on localhost, will try it now on server! Any custom specification need to be defined on server or not?

No there are no specific configurations needed for production, you can access it the same way as you did in development, Make sure you only access public data 👍

abhinavvsinhaa commented 3 years ago

Sure @abhinav-sinhaa, I currently have this working for my Node JS app in production, A Heroku Server in US. They are working fine. For now you can only do GET Requests, Kindly let me know if you face any issues. 🦄

It's showing proxy server is not connected!

You have to connect the client, only then the PROXY will be connected, if you are calling the functions right away, You have to add a buffer time to get you enough time to connect it, It is basically doing the small scale process of proxy servers, What it will do is it will use your IP to get the data that is restricted to a area, Also you have to persist the session to maintain contact, This is a small scale version of proxy servers as said, Kindly refer this section https://github.com/saran-surya/cassata#accessing-the-proxy-client-, You have to connect the proxy client and then access the data. This should help.

Working fine on localhost, will try it now on server! Any custom specification need to be defined on server or not?

No there are no specific configurations needed for production, you can access it the same way as you did in development, Make sure you only access public data 👍

Man thanks a tonnnn! You literally saved my life. I'm checking for some bugs, after that I'll tell you the final updates!!Thanks again @saran-surya

abhinavvsinhaa commented 3 years ago

@saran-surya Thanks a lot it's working fine on the server.

abhinavvsinhaa commented 3 years ago

Sure @abhinav-sinhaa, I currently have this working for my Node JS app in production, A Heroku Server in US. They are working fine. For now you can only do GET Requests, Kindly let me know if you face any issues. 🦄

It's showing proxy server is not connected!

You have to connect the client, only then the PROXY will be connected, if you are calling the functions right away, You have to add a buffer time to get you enough time to connect it, It is basically doing the small scale process of proxy servers, What it will do is it will use your IP to get the data that is restricted to a area, Also you have to persist the session to maintain contact, This is a small scale version of proxy servers as said, Kindly refer this section https://github.com/saran-surya/cassata#accessing-the-proxy-client-, You have to connect the proxy client and then access the data. This should help.

Working fine on localhost, will try it now on server! Any custom specification need to be defined on server or not?

No there are no specific configurations needed for production, you can access it the same way as you did in development, Make sure you only access public data 👍

Man thanks a tonnnn! You literally saved my life. I'm checking for some bugs, after that I'll tell you the final updates!!Thanks again @saran-surya

Whenever I close the proxyrouter it automatically disconnects. What's the solution for that?

saran-surya commented 3 years ago

Sure @abhinav-sinhaa, I currently have this working for my Node JS app in production, A Heroku Server in US. They are working fine. For now you can only do GET Requests, Kindly let me know if you face any issues. 🦄

It's showing proxy server is not connected!

You have to connect the client, only then the PROXY will be connected, if you are calling the functions right away, You have to add a buffer time to get you enough time to connect it, It is basically doing the small scale process of proxy servers, What it will do is it will use your IP to get the data that is restricted to a area, Also you have to persist the session to maintain contact, This is a small scale version of proxy servers as said, Kindly refer this section https://github.com/saran-surya/cassata#accessing-the-proxy-client-, You have to connect the proxy client and then access the data. This should help.

Working fine on localhost, will try it now on server! Any custom specification need to be defined on server or not?

No there are no specific configurations needed for production, you can access it the same way as you did in development, Make sure you only access public data 👍

Man thanks a tonnnn! You literally saved my life. I'm checking for some bugs, after that I'll tell you the final updates!!Thanks again @saran-surya

Whenever I close the proxyrouter it automatically disconnects. What's the solution for that?

Thanks a lot for the response, Yes you have to have the proxyrouter open, for the period of time you need the server to use the proxy, for security reasons, the connection between the proxy and the server will be disconnected on RELOAD / CLOSING the active proxyrouter session, Not mandatory that you should have that running top of your screen, you can have it running in the background also, As said a smaller version of proxy routers, thus as long as your local machine is connected to the internet, and connected to the proxyrouter, your connection will be persistant. 👍🏻

Kindly refer this.. Client connection

abhinavvsinhaa commented 3 years ago

Thank you again for help @saran-surya @kumaraswins . Now the issue is resolved so I'm closing it.