【问题标题】:How to get only part of API data?如何只获取部分 API 数据?
【发布时间】:2021-12-18 08:14:48
【问题描述】:

我正在使用以下代码获取 Marvel API 数据:

const axios = require('axios');

const md5 = require('blueimp-md5');
const publickey = 'publickey';
const privatekey = 'privatekey';
const ts = new Date().getTime();
const stringToHash = ts + privatekey + publickey;
const hash = md5(stringToHash);
const baseUrl = 'https://gateway.marvel.com:443/v1/public/characters';
const url = baseUrl + '?ts=' + ts + '&apikey=' + publickey + '&hash=' + hash;

async function getMarvel(){
    const { data } = await axios.get(url);
    return data; 
}

以下是输出的片段:

{
        "id": 1009150,
        "name": "Agent Zero",
        "description": "",
        "modified": "1969-12-31T19:00:00-0500",
        "thumbnail": {
          "path": "http://i.annihil.us/u/prod/marvel/i/mg/f/60/4c0042121d790",
          "extension": "jpg"
},
  
{
    "id": 1011031,
    "name": "Agent X (Nijo)",
    "description": "Originally a partner of the mind-altering assassin Black Swan, Nijo spied on Deadpool as part of the Swan's plan to exact revenge for Deadpool falsely taking credit for the Swan's assassination of the Four Winds crime family, which included Nijo's brother.",
    "modified": "1969-12-31T19:00:00-0500",
    "thumbnail": {
      "path": "http://i.annihil.us/u/prod/marvel/i/mg/b/40/image_not_available",
      "extension": "jpg"
    },

如果我只需要 API 调用中的零代理,我可以这样做:

const baseUrl = 'https://gateway.marvel.com:443/v1/public/characters?name=Agent+Zero';

这会引发 409 错误,我可以更改什么以仅获取一个代理数据?

【问题讨论】:

  • 如果您想从非常简单的响应中得到它,但如果您想知道如何让它只返回一个数据点,那么您需要弄清楚来自 API 文档。
  • 这与您的previous question 有何不同?
  • @Phil 你看到上一个问题的回复了吗?我也没有
  • 这并不意味着“发布一个新问题”
  • 我想我在这里解释得更好

标签: node.js api express axios routes


【解决方案1】:

是语法问题,

这个:

const url = baseUrl + '?ts=' + ts + '&apikey=' + publickey + '&hash=' + hash;

应该是:

const url = baseUrl + '&ts=' + ts + '&apikey=' + publickey + '&hash=' + hash;

在 ts 中用 & 号代替问号

【讨论】:

    猜你喜欢
    • 2018-08-12
    • 2019-01-16
    • 2013-07-07
    • 2022-08-16
    • 2019-11-19
    • 1970-01-01
    • 2017-12-06
    • 2017-04-24
    • 2011-08-27
    相关资源
    最近更新 更多