【问题标题】:API header parameters returns "SyntaxError: Unexpected token"API 标头参数返回“SyntaxError: Unexpected token”
【发布时间】:2020-06-01 19:12:08
【问题描述】:

我正在开发一个小型 web 应用程序来自学一些 gcloud 和 js。目前正在尝试使用 axios 从谷歌云功能向 Web api 发送获取请求。任何指导将不胜感激。

web api在header中需要以下参数 接受:应用程序/json APIKey:APIKEY

我一直在尝试使用How to set header and options in axios? 中的代码 和 axios 信息,但我不断收到错误 (SyntaxError: Unexpected token)

Index.JS 目前看起来是这样的

const axios = require("axios");

exports.run = async(req, res) => {

    axios.get('APIURL', {
     headers: {
       'Accept': 'application/json';
       'APIKey': 'KEY'
     }
    }).then((response) => {
        res.status(200).send(response.data);
        console.log(response);
      }, (error) => {
        res.status(500).send(response.data);
        console.log(error);
      });

     };

package.json

{
  "name": "YOUR_NAME",
  "version": "0.0.1",
  "dependencies": {
    "axios": "^0.19.2"
  }
}

【问题讨论】:

  • 请编辑问题以显示完整的错误消息,并指出它所指的代码行。
  • 错字:你使用了;,而你应该使用,

标签: javascript google-cloud-platform axios google-cloud-functions


【解决方案1】:

这不是一个有效的 JSON 对象:

     headers: {
       'Accept': 'application/json';
       'APIKey': 'KEY'
     }

分号不合适。请改用逗号:

     headers: {
       'Accept': 'application/json',
       'APIKey': 'KEY'
     }

【讨论】:

  • 这也不是有效的 JSON。属性名称必须是字符串,而不是标识符,并且字符串必须用双引号而不是单引号引起来。
  • ... 查看上下文,it isn't supposed to be JSON anyway
  • 感谢您的意见!我可以发誓我在那里有一个逗号。在创建这篇文章之前,我确实在记事本中格式化了代码,所以我可能只是在之后输入了它。很抱歉造成混乱但我仔细检查了所有内容,仍然出现错误,我的问题是我不确定如何传递标头信息......并且第一次使用 java,有很多新东西,但首先跳入脑海。错误看起来像这样错误:函数终止。函数无法初始化。
猜你喜欢
  • 1970-01-01
  • 2019-08-07
  • 1970-01-01
  • 1970-01-01
  • 2017-12-09
  • 2018-11-29
  • 2012-02-23
  • 1970-01-01
  • 2023-04-08
相关资源
最近更新 更多