【问题标题】:Zomato API not working - ReactJsZomato API 不工作 - ReactJs
【发布时间】:2018-04-07 22:26:00
【问题描述】:

我很难在 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。找不到任何说明来弄清楚我哪里出错了......

谢谢, 蕾娜。

【问题讨论】:

    标签: javascript reactjs api zomato-api


    【解决方案1】:

    我知道了,答案是这样的:

    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 }); }); 
    

    谢谢大家。

    【讨论】:

      【解决方案2】:

      从 Headers 中传递 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);
            });
      

      【讨论】:

        【解决方案3】:

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

        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"
        

        希望这会对你有所帮助。

        【讨论】:

        • 好的,但是我会用 axios 写谁呢?我不认为我在这个项目中使用 curl...
        • 在您的 axios 请求中,只需在标题中设置“X-Zomato-API-Key:”即可。
        • 我知道了,答案是这样的: const config = { headers: {'user-key': 'MY KEY HERE'} }; axios.get('developers.zomato.com/api/v2.1/…', config) .then(res => { console.log(res.data.collections); this.setState( { places: res.data.collections }); });谢谢大家。
        【解决方案4】:

        您似乎缺少user-key 参数及其值。例如,URL 应该是这样的:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-02-26
          • 1970-01-01
          • 2016-09-24
          • 2018-10-09
          • 2021-12-27
          • 2022-08-07
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多