首页 文章

从React中的API获取 Headers 中的键,返回406是不可接受的

提问于
浏览
0

我正在尝试使用API密钥从API获取,但仍然获得406 Not Acceptable . 我可以获得在Postman中工作的请求,但不能在代码中 . 这可能有什么问题?我已经尝试了各种方法将我的信息包含在 Headers 中,这只是我的最后一次尝试 .

componentDidMount() {
    fetch("my-api", {
      method: "GET",
      headers: ({
      Accept: "application/json",
      "Content-Type": "application/json",
      "X-Api-Version": 20161108,
      Authorization: {
          Token: "my-api-key",
      }
     }),
      body: JSON.stringify()
    }).then(response => {
      if (response.status === 201) {
        console.log(response)
        return response.json()
      } else {
        console.log("oh no!", response.status === 404)
      }
    })
  }

1 回答

  • 1

    我想到了 . 最终工作:

    componentDidMount() {
            fetch("my api", {
              method: "GET",
              headers: ({
              Accept: "application/vnd.api+json",
              "Content-Type": "application/json",
              "X-Api-Version": 20161108,
              Authorization: "Token my token",
             }),
              body: JSON.stringify()
            }).then(response => {
              if (response.status === 200) {
                console.log(response)
                return response.json()
              } else {
                console.log("oh no!", response.status === 404)
              }
            })
          }
    

相关问题