node.js - 无法使用 NodeJS 在 Marvel API 中搜索角色名称
问题描述
我正在使用 Marvel Api 获取字符数据。我正在使用 axios 来获取这些数据,下面是代码:
const axios = require('axios');
const md5 = require('blueimp-md5');
const publickey = '73f5271b4d972dc0f4eba';
const privatekey = 'e9499a5ab5e65';
const ts = new Date().getTime();
const stringToHash = ts + privatekey + publickey;
const hash = md5(stringToHash);
sp = 'Spider-man'
const baseUrl = 'https://gateway.marvel.com:443/v1/public/comics';
const url = baseUrl + '?ts=' + ts + '&apikey=' + publickey + '&hash=' + hash;
async function getMarvel(){
const { data } = await axios.get(url);
return data; // this will be the array of people objects
}
module.exports = {
getMarvel
}
路线:
const express = require('express');
const router = express.Router();
const data = require('../data');
const marvelData = data.marvel;
router.get('/', async (req, res) => {
try {
const marv = await marvelData.getMarvel();
res.json(marv);
} catch (e) {
res.status(404).json({ message: e.message });
}
});
module.exports = router;
应用程序.js
const express = require('express');
const app = express();
const configRoutes = require('./routes');
configRoutes(app);
app.listen(3000, () => {
console.log("We've now got a server!");
console.log('Your routes will be running on http://localhost:3000');
});
当查询字符串 URL 为:
'https://gateway.marvel.com:443/v1/public/characters'
数据加载并且工作正常,但是当我尝试按名称搜索它时它不起作用:
'https://gateway.marvel.com:443/v1/public/characters?name=Spider-Man'
它返回 409 错误,不确定我错过了什么。
当我将名称添加到 Marvel api 的交互式文档时的输出:
{
"code": 200,
"status": "Ok",
"copyright": "© 2021 MARVEL",
"attributionText": "Data provided by Marvel. © 2021 MARVEL",
"attributionHTML": "<a href=\"http://marvel.com\">Data provided by Marvel. © 2021 MARVEL</a>",
"etag": "79ef3436d0dc139b17693635b99776556e29f495",
"data": {
"offset": 0,
"limit": 20,
"total": 0,
"count": 0,
"results": []
}
}
解决方案
在您的第一个代码存根中
const url = baseUrl + '&ts=' + ts + '&apikey=' + publickey + '&hash=' + hash;
使它&ts
而不是?ts
作为连续查询参数应该以&
推荐阅读
- laravel - Laravel 使用 assertJson 测试集合 JSON 响应
- java - 无法在 JavaSpring 中自动装配
- excel - 将一列中的数据重新排列为多行
- c++ - 编译时间常数确定
- php - 如何获取特定字符串后的数据
- sas - 此 SQL 查询的 DATA 步等效项是什么?
- android - 如何从 Firestore 集合中获取所有文档并将它们返回到数组列表中?
- connection - 由于访问问题,无法在 Azure 数据工厂中创建连接
- drop-down-menu - 材质组件菜单的 `setAbsolutePosition()` 无法正常工作?
- uml - Doxygen 提取数据类型并显示错误的关联性