Zomato API无法正常工作 - ReactJs

时间:2018-04-07 22:26:01

标签: javascript reactjs api zomato-api

我很难在reactjs中尝试制作基本的zomato api请求。

api文档看起来很简单。我正在对类别进行基本的GET请求:https://developers.zomato.com/documentation#!/common/categories

以下是我的ReactJS函数的样子:

  componentDidMount() {
   // A previous request for london restaurants 
  //  axios.get('https://developers.zomato.com/api/v2.1/geocode? 
 lat=51.5138&lon=0.0984')

 axios.get('https://developers.zomato.com/api/v2.1/categories')
  .then(res => {
    console.log(res.data);
    this.setState(
      { places: res.data});
  });
 }

然而,当我在浏览器中点击任何api url时,我会不断收到此响应。或者通过失眠。

{
"code": 403,
"status": "Forbidden",
"message": "Invalid API Key"
}

我知道它说无效的API。但是我在登录并在Zomato的开发人员门户中应用任何API密钥后获得了这些URL。找不到任何说明我想出错的地方......

谢谢, 里纳。

4 个答案:

答案 0 :(得分:2)

我明白了,答案是:

const config = { headers: {'user-key': 'MY KEY HERE'} }; 
enter code here
axios.get('developers.zomato.com/api/v2.1/…;, config) .then(res => { 
console.log(res.data.collections); this.setState( { places: 
res.data.collections }); }); 
谢谢大家。

答案 1 :(得分:0)

您需要在请求标头中设置此密钥。

  

X-Zomato-API-Key:

样品申请:

curl -X GET --header "Accept: application/json" --header "X-Zomato-API-Key: <YOUR_API_KEY>" "https://developers.zomato.com/api/v2.1/categories"

希望这会对你有所帮助。

答案 2 :(得分:0)

似乎您缺少user-key参数及其值。例如,URL应为:

https://developers.zomato.com/api/v2.1/categories?user-key=<your API key>

答案 3 :(得分:0)

从标头传递API_Key,您将获得响应数据。

axios({
      method: "GET",
      url: "https://developers.zomato.com/api/v2.1/search",
      headers: {
        "user-key": "b8cc3b8b0a85afed047f030fb52dc15f",
        "content-type": "application/json"
      }
    })
      .then(response => {
        console.log(response.data.restaurants[0].restaurant.name);
      })
      .catch(error => {
        console.log(error);
      });