正在向第三方 API 发送带有 GET 请求的 Headers
Sending Headers with a GET request to Third Party API
我正在尝试构建一个小型 full-stack 应用程序,其中我的前端代码将只与我的后端交互,而后端将调用第三方 API 来获取结果然后执行任何操作想要 it.The 第三方 API 我正在使用需要 Accept
, app_id
和 app_key
headers 任何请求。
我可以使用 Postman 直接向我的第三方 API 发送 GET
请求并获取结果就好了,但是当我尝试通过我的 NodeJS 发送 GET
请求时 API,我收到的是 status 403
。为此,我正在使用 node-fetch
。
我用的API是Oxford Dictionary API
这是我的 index.js
文件的代码
require('dotenv').config();
const express = require('express');
const router = require('./routes/router');
const cors = require('cors');
const app = express();
const port = process.env.PORT || '5000';
const host = process.env.HOST || 'localhost';
app.use(cors());
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(router);
app.listen(port, () => console.log(`server is up and running at http://${host}:${port} `));
这是我的 router.js
文件的代码。
const express = require('express')
const fetch = require('node-fetch');
const router = express.Router();
const BASE_URL = process.env.BASE_URL;
const fields = 'definitions';
router.get('/', (req, res) => {
res.json({
status: "successfully connected !"
});
});
router.post('/create', (req, res) => {
console.log(req.body);
console.log(req.body.word);
const url = `${BASE_URL}entries/en-gb/${req.body.word}?fields=${fields}&strictMatch=true`;
const newHeaders = {
"Accept": "application/json",
"app_id": process.env.APP_ID,
"app_key": process.env.API_KEY
}
fetch(url, { method: 'GET', headers: { newHeaders } })
.then((results) => {
console.log(results);
}).then(results => {
res.json(results);
}).catch((err) => { console.log(err) });
});
module.exports = router;
以下是我收到的results
:-
Response {
size: 0,
timeout: 0,
[Symbol(Body internals)]: {
body: PassThrough {
_readableState: [ReadableState],
_events: [Object: null prototype],
_eventsCount: 5,
_maxListeners: undefined,
_writableState: [WritableState],
allowHalfOpen: true,
[Symbol(kCapture)]: false,
[Symbol(kTransformState)]: [Object]
},
disturbed: false,
error: null
},
[Symbol(Response internals)]: {
url: 'https://od-api.oxforddictionaries.com/api/v2/entries/en-gb/swim?fields=definitions&strictMatch=true',
status: 403,
statusText: 'Forbidden',
headers: Headers { [Symbol(map)]: [Object: null prototype] },
counter: 0
}
}
放心,environment variables
都设置了准确的值。
我想要附加 headers
并向第三方 API 发送 GET
请求并成功获取结果。另外,我希望能够将这些 results
发送回我的客户。
我对我的代码有几个问题。
- 在快速路线内使用
fetch
是否正确?
- 如果是,为什么返回
status 403
?
- 如果不是,实现此目标的正确方法是什么?
- 这个
headers: Headers { [Symbol(map)]: [Object: null prototype] },
是什么意思?
我是网络开发的新手,所以这个问题对这里的一些老手来说可能看起来很愚蠢,但请帮助我。我已阅读以下资源,但其中 none 对我有帮助:-
Node-Fetch API GET with Headers
How to fetch from third party API inside backend route?
编辑:
我用了app-id
和app-key
,而不是app_id
和app_key
,我现在已经更正了,但问题还没有解决。
正如我在评论中提到的,您打错了字:
headers: { newHeaders }
-- newHeaders
已经是一个对象,所以使用 headers: newHeaders
.
我正在尝试构建一个小型 full-stack 应用程序,其中我的前端代码将只与我的后端交互,而后端将调用第三方 API 来获取结果然后执行任何操作想要 it.The 第三方 API 我正在使用需要 Accept
, app_id
和 app_key
headers 任何请求。
我可以使用 Postman 直接向我的第三方 API 发送 GET
请求并获取结果就好了,但是当我尝试通过我的 NodeJS 发送 GET
请求时 API,我收到的是 status 403
。为此,我正在使用 node-fetch
。
我用的API是Oxford Dictionary API
这是我的 index.js
文件的代码
require('dotenv').config();
const express = require('express');
const router = require('./routes/router');
const cors = require('cors');
const app = express();
const port = process.env.PORT || '5000';
const host = process.env.HOST || 'localhost';
app.use(cors());
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(router);
app.listen(port, () => console.log(`server is up and running at http://${host}:${port} `));
这是我的 router.js
文件的代码。
const express = require('express')
const fetch = require('node-fetch');
const router = express.Router();
const BASE_URL = process.env.BASE_URL;
const fields = 'definitions';
router.get('/', (req, res) => {
res.json({
status: "successfully connected !"
});
});
router.post('/create', (req, res) => {
console.log(req.body);
console.log(req.body.word);
const url = `${BASE_URL}entries/en-gb/${req.body.word}?fields=${fields}&strictMatch=true`;
const newHeaders = {
"Accept": "application/json",
"app_id": process.env.APP_ID,
"app_key": process.env.API_KEY
}
fetch(url, { method: 'GET', headers: { newHeaders } })
.then((results) => {
console.log(results);
}).then(results => {
res.json(results);
}).catch((err) => { console.log(err) });
});
module.exports = router;
以下是我收到的results
:-
Response {
size: 0,
timeout: 0,
[Symbol(Body internals)]: {
body: PassThrough {
_readableState: [ReadableState],
_events: [Object: null prototype],
_eventsCount: 5,
_maxListeners: undefined,
_writableState: [WritableState],
allowHalfOpen: true,
[Symbol(kCapture)]: false,
[Symbol(kTransformState)]: [Object]
},
disturbed: false,
error: null
},
[Symbol(Response internals)]: {
url: 'https://od-api.oxforddictionaries.com/api/v2/entries/en-gb/swim?fields=definitions&strictMatch=true',
status: 403,
statusText: 'Forbidden',
headers: Headers { [Symbol(map)]: [Object: null prototype] },
counter: 0
}
}
放心,environment variables
都设置了准确的值。
我想要附加 headers
并向第三方 API 发送 GET
请求并成功获取结果。另外,我希望能够将这些 results
发送回我的客户。
我对我的代码有几个问题。
- 在快速路线内使用
fetch
是否正确? - 如果是,为什么返回
status 403
? - 如果不是,实现此目标的正确方法是什么?
- 这个
headers: Headers { [Symbol(map)]: [Object: null prototype] },
是什么意思?
我是网络开发的新手,所以这个问题对这里的一些老手来说可能看起来很愚蠢,但请帮助我。我已阅读以下资源,但其中 none 对我有帮助:-
Node-Fetch API GET with Headers
How to fetch from third party API inside backend route?
编辑:
我用了app-id
和app-key
,而不是app_id
和app_key
,我现在已经更正了,但问题还没有解决。
正如我在评论中提到的,您打错了字:
headers: { newHeaders }
-- newHeaders
已经是一个对象,所以使用 headers: newHeaders
.