You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Axios发送POST请求实现Discord机器人Nike订单查询?

Nike订单查询Discord机器人实现方案

一、Axios发送POST请求的核心用法

要发送携带邮箱和订单号的POST请求,核心是使用axios.post()方法,传入目标接口URL、请求参数,同时配置符合Nike反爬要求的请求头:

const response = await axios.post(targetUrl, {
  // 注意:参数名需和Nike接口要求一致,可能需要抓包确认
  emailAddress: 邮箱参数,
  orderNumber: 订单号参数
}, {
  headers: {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36',
    'Content-Type': 'application/json'
  }
});

二、修改后的完整代码

const { CommandInteraction, MessageEmbed } = require('discord.js');
const axios = require('axios');
const wait = require('node:timers/promises').setTimeout;

module.exports = {
    name: 'status',
    description: '查询Nike订单状态',
    permission: 'USE_APPLICATION_COMMANDS',
    options: [
        {
            name: 'email',
            description: '输入订单关联邮箱',
            required: true,
            type: 'STRING'
        },
        {
            name: 'ordernumber',
            description: '输入订单编号',
            required: true,
            type: 'STRING'
        },
    ],

    /**
     * @param {CommandInteraction} interaction 
     */
    async execute(interaction) {
        const { options } = interaction;
        const email = options.getString("email");
        const orderNumber = options.getString("ordernumber");
        // 实际接口需抓包确认,此处为示例路径
        const queryUrl = 'https://www.nike.com/api/orders/details';

        try {
            // 先发送加载状态,避免Discord判定命令超时
            await interaction.deferReply();
            await wait(1000);

            // 发送POST请求查询订单
            const response = await axios.post(queryUrl, {
                emailAddress: email,
                orderNumber: orderNumber
            }, {
                headers: {
                    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36',
                    'Accept': 'application/json',
                    'Content-Type': 'application/json'
                }
            });

            // 从响应中提取订单数据(需根据Nike返回的JSON结构调整)
            const orderData = response.data;
            const orderStatus = orderData.status || '未知';
            const productName = orderData.items?.[0]?.name || '未获取到';
            const orderDate = orderData.orderDate || '未获取到';
            const size = orderData.items?.[0]?.size || '未获取到';

            // 构建返回的Embed消息
            const orderEmbed = new MessageEmbed()
                .setTitle('Nike订单查询结果 🧪')
                .setURL(`https://www.nike.com/orders/details/${orderNumber}`)
                .addFields(
                    { name: '**关联邮箱**', value: `${email}` },
                    { name: '**订单状态**', value: `${orderStatus}` },
                    { name: '**商品名称**', value: `${productName}`, inline: true },
                    { name: '**下单日期**', value: `${orderDate}`, inline: true },
                    { name: '**订单编号**', value: `${orderNumber}`, inline: true },
                    { name: '**商品尺码**', value: `${size}` },
                )
                .setTimestamp();

            // 更新回复发送结果
            await interaction.editReply({ embeds: [orderEmbed] });
        } catch (error) {
            // 处理各类错误情况
            let errorMsg = '查询失败,请检查邮箱和订单号是否正确';
            if (error.response?.status === 404) {
                errorMsg = '未找到该订单,请确认信息无误';
            } else if (error.response?.status === 403) {
                errorMsg = '请求被拦截,请调整请求头或稍后重试';
            }

            await interaction.editReply({ content: errorMsg });
            console.error('订单查询错误:', error);
        }
    }
};

三、关键注意事项

  • 接口验证:Nike的访客订单查询接口可能不是你最初使用的路径,需要通过浏览器开发者工具抓包,获取实际的POST接口地址、参数名和响应格式。
  • 反爬规避:必须配置真实的浏览器请求头,尤其是User-Agent,否则容易被Nike的反爬机制拦截。
  • 交互优化:使用deferReply()先回复加载状态,避免Discord因命令超时触发警告,后续用editReply()更新最终结果。

内容的提问来源于stack exchange,提问作者JediSZN

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 04:15:30