【问题标题】:When using request it's returning random strings and letters instead of XML使用请求时,它返回随机字符串和字母而不是 XML
【发布时间】:2020-04-29 04:43:27
【问题描述】:

我正在尝试检索此 API 的 XML:https://www.roblox.com/asset/?id=4562735369,当我使用邮递员时,它会正确返回 XML。

我想得到什么:

<roblox xmlns:xmime="http://www.w3.org/2005/05/xmlmime" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.roblox.com/roblox.xsd" version="4">
  <External>null</External>
  <External>nil</External>
  <Item class="Decal" referent="RBX0">
    <Properties>
      <token name="Face">5</token>
      <string name="Name">Decal</string>
      <float name="Shiny">20</float>
      <float name="Specular">0</float>
      <Content name="Texture">
        <url>http://www.roblox.com/asset/?id=4562735350</url>
      </Content>
      <bool name="archivable">true</bool>
    </Properties>
  </Item>
</roblox>

我得到了什么: �u�_K�0���Cɻ�u[F� L��^��n �IIҵ~{�6��9�Czί���PkVʴI[*�fm)K�I�}5h�f�LF�na���'`B���g�kX�����_I�&lt;ע��ə6o�DWq��?7�{i��I�o��)![�&amp;����)a�IB�Z�Vs�t����S�x/�D(�\NQpE���}N&gt;���wk*�^��Jм�B�h޵�6!,�ЋG�y+�6B�΄�A�Hn��&gt;��B�o��)�^&gt;CU(j�-a�܃�&gt;l�Ol}m��@�V���u��;�Χ���v�M�������1*�q+ ��+���Bg?UR�n!i�d����\�

我的代码:

// Packages
const express = require("express");
const xmlparser = require("express-xml-bodyparser");
const getQueryParam = require("get-query-param");
const request = require("request");
const rateLimit = require("express-rate-limit");
const colors = require("colors");

// Configuration
const PORT = 3000;
const RATE_LIMIT_WINDOW_TIME = 15 * 60 * 1000; // 15 minutes
const RATE_LIMIT_REQUEST_MAX = 100;

// Functions
function logInformation(tag, data) {
  console.log(colors.yellow(`[${tag}]: `) + colors.underline(data));
}

// express app
const limiter = rateLimit({
  windowMs: RATE_LIMIT_WINDOW_TIME,
  max: RATE_LIMIT_REQUEST_MAX
});

const app = express();
logInformation("Startup", "Created express server instance.");

app.use(limiter);
logInformation("Startup", "Started using app rate limiter");

app.use(xmlparser());
logInformation("Startup", "Started using XML body parser");

// api

app.get("/getDecalID/:id", (req, res) => {
  let imageId = req.params.id;

  if (imageId) {
    request(`http://www.roblox.com/asset/?id=4562735369`, function(
      error,
      response,
      body
    ) {
      if (!error) {
        logInformation("Body", body);
        res.send(body);
      } else {
        res
          .status(500)
          .json({ error: "Error while sending request to roblox's API" });
      }
    });
  } else {
    res.status(500).json({ error: "imageId not provided or found." });
  }
});

// Listen
app.listen(PORT, () => {
  console.log(colors.green("Running: ") + `http://localhost:${PORT}`);
});

邮递员截图:

我不确定还能做什么,因为每次我发送请求时它只会返回随机字符。我需要 XML,以便将其解析为数据。我知道这是可能的,因为邮递员能够获取 XML,但我不知道为什么我不能得到它。请帮忙。

【问题讨论】:

    标签: node.js xml npm request postman


    【解决方案1】:

    Request 不允许您使用 XML 并对其进行解析,因此我发现 axios 与 request 非常相似并返回 XML 数据。

    【讨论】:

      猜你喜欢
      • 2019-01-19
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 2020-04-29
      • 1970-01-01
      • 2018-02-28
      • 2013-02-23
      相关资源
      最近更新 更多