S
S
sawuer2019-10-14 20:29:34
JavaScript
sawuer, 2019-10-14 20:29:34

What is the correct way to fetch data via fetch() along with a header from a third party API?

Using Postman, I can get data at the address " https://api.weather.yandex.ru/v1/forecast?lang=ru&... " and with the header "X-Yandex-API-Key: <here is my api key>":
5da4ae64a11d0787438535.png
I do the same, but with fetch():

fetch('https://api.weather.yandex.ru/v1/forecast?lang=ru'+ '&lat=' + this.props.city.lat + '&lon=' + this.props.city.lon, {
      headers: (new Headers({
        'X-Yandex-API-Key': API_KEY
      }));
    })
      .then(resp => resp.json())
      .then(res => {
        console.log(res)
      });

I get code 200 and empty response:
5da4afcc9b800507401201.png
What could be the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
2
2bllk, 2019-10-14
@2bllk

Maybe you should try to specify the method: " method: 'GET' "?

fetch('https://api.weather.yandex.ru/v1/forecast?lang=ru'+ '&lat=' + this.props.city.lat + '&lon=' + this.props.city.lon, {
    method: 'GET',
    headers: (new Headers({
        'X-Yandex-API-Key': API_KEY
    }))
}).then(resp => resp.json()).then(res => {
    console.log(res)
});

For I see in your last screenshot it is indicated that the request is made using the OPTIONS method , and should be done using the GET method

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question