无法使用 NodeJS 在 Marvel API 中搜索角色名称 [英] Not able to search for character name in Marvel API using NodeJS

查看:20
本文介绍了无法使用 NodeJS 在 Marvel API 中搜索角色名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Marvel Api 来获取角色数据.我正在使用 axios 来获取这些数据,以下是代码:

I am using the Marvel Api to get characters data. I am using axios to get this data, below is the code:

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;

app.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 为:

When the query string URL is:

'https://gateway.marvel.com:443/v1/public/characters'

数据加载并且工作正常,但是当我尝试按名称搜索它时它不起作用:

The data loads and works fine, but when I try to search it by name it does not work:

'https://gateway.marvel.com:443/v1/public/characters?name=Spider-Man'

它返回 409 错误,不确定我错过了什么.

It returns a 409 error, not sure what am I missing.

当我将名称添加到 marvel api 的交互式文档时的输出:

Output when I add name to Interactive documentation of 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 因为继续查询参数应该以 &

make it &ts instead of ?ts as continuing query parameters should start with &

这篇关于无法使用 NodeJS 在 Marvel API 中搜索角色名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆