AstronomyAPI / Samples

Examples for Astronomy API
https://astronomyapi.com
MIT License
30 stars 3 forks source link

can't get fetch to work #62

Closed gitxandert closed 10 months ago

gitxandert commented 10 months ago

Hi, I'm trying to access data from here, but I don't see any examples that use the fetch API to do so, and I consistently get denied access when trying to fetch data.

Here's my code: const authString = btoa(myApplicationId:myApplicationSecret);

fetch('https://api.astronomyapi.com/api/v2/bodies/positions', { header: Authorization: Basic ${authString}, method: "GET", mode: "cors", }) .then(function(response) { return response.json(); console.log(response.json()); });

I'm pretty new to this stuff and am not sure how much of what I have here is necessary or not; regardless, I can't get any data from this site.

Thanks in advance!

gitxandert commented 10 months ago

After looking to see if anybody else here has used fetch, I changed my code:

fetch('https://api.astronomyapi.com/api/v2/bodies/positions', { method: "GET", mode: "cors", headers: {"Content-Type":'application/json', Authorization: Basic ${authString},}}) .then(function(response) { return response.json(); console.log(response.json()); });

Now I'm getting a 422 error... Could really use some help!

gitxandert commented 10 months ago

Hi, still trying to figure this out.

I looked at the documentation more carefully and made this:

fetch("https://api.astronomyapi.com/api/v2/bodies/positions", { method: "GET", headers: { 'Content-Type' : 'application/json', Authorization : Basic ${authString}, }, data: { "latitude" : 39.163587, "longitude" : -86.499374, "elevation" : 243, "from_date" : "2023-11-14", "to_date" : "2023-11-14", "time" : "18:30:00" }, });

This still does not work; I am still getting a 422 error. I am very confused and would really appreciate some help.

astroanu commented 10 months ago

Hi, in a GET call there's no data (this should actually be body in fetch api. The correct way to use fetch is this.

fetch("https://api.astronomyapi.com/api/v2/bodies/positions?" + new URLSearchParams({
    "latitude": '39.163587',
    "longitude": '-86.499374',
    "elevation": '243',
    "from_date": "2023-11-14",
    "to_date": "2023-11-14",
    "time": "18:30:00"
  }).toString()
    , {
      method: "GET",
      headers: {
        'Content-Type': 'application/json',
        Authorization: `Basic xxx`,
      }
    }); 
gitxandert commented 10 months ago

Thank you so much!! I finally have data and am able to process it as a JSON. :)

I'm sorry that my question is so amateurish; I am very very new to web development and am mainly learning it for an electronic music thesis implementing Max MSP's RNBO package. I would like to use this API's data as parameters for a website that continously generates music by updating musical parameters with astronomical ones, so that, as the skies change, so does the music. Do you have any advice on how to assign the fetched data to script variables?

Thank you again!

astroanu commented 10 months ago

I'm not sure about the algorithm, but i'd say you're going there.

Since the sky doesn't change much over a few minutes, it's good to just make a call to the api once an hour or so, and based on what's visible come up with an algorithm to generate music?

Maybe you can also try the new events api https://docs.astronomyapi.com/endpoints/bodies/events which will give you eclipse information.

gitxandert commented 10 months ago

Thanks for your advice about how often to call the API! I'll definitely take a look at all of the APIs and see which data I could best sonify. Is it okay if I ask you more questions later?