javascript - 向 Fluxpoint api 发送请求时出错

标签 javascript node.js api request

又是我...抱歉每天问这么多次,但我真的是个白痴。

所以基本上我正在尝试使用以下代码向 Fluxpoint api 发送请求:

async welcome(username, avatarurl, background, membercount, icon, backgroundname, filename){
        let req = {}
        req.username = username;
        req.avatar = avatarurl;
        if (background == null) {req.background = "#aaaaaa"} else {req.background = background}
        if (membercount) req.members = "Member #"+membercount
        if (icon) req.icon = icon
        if (backgroundname) req.banner = backgroundname
        console.log(req)
        let usedClient = axios.create({
            baseURL: apiUrls[0],
            timeout: 5000,
            headers: {
                'Authorization': this.token,
                'Content-Length': 0,
                'Content-Type': 'application/json'
            },
            data: JSON.parse(req),
            responseType: 'arraybuffer'
        })
        console.log(usedClient)
        console.log(apiUrls[0]+api1endpoints[1])
        let res = await usedClient.get(api1endpoints[1])
        return res
    }

这是我用来测试它的代码:

const fluxpoint = require('./index')
const Client = new fluxpoint.Client("my fluxpoint token")

async function tt(){
    let t = await Client.welcome("Koro~ (Baka)#7963", "https://cdn.discordapp.com/avatars/304541381798658048/36806f6ae648b9ebc8303443b0be101c.png", "#FFFFFF", 1, "neko", "space")
    console.log(t)
}
tt()

并且,这是 Fluxpoint api 发送给我的错误:

无法解析 json,输入不包含任何 JSON 标记。当 isFinalBlock 为 true 时,排除以有效 JSON token 开头的输入。路径: $ |行号: 0 |字节位置内联:0。

我尝试了所有方法,但 JSON.parse(我的数据) 向我发送了 JSON 中位置 1 处的无异常标记 o

我很绝望,希望有人能帮助我!

最佳答案

您似乎正在解析原始 json。它会抛出错误

JSON.parse 将字符串作为参数。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

根据官方文档,您不能在 get 请求中使用数据。

https://github.com/axios/axios#request-config

 // `data` is the data to be sent as the request body
  // Only applicable for request methods 'PUT', 'POST', and 'PATCH'
  // When no `transformRequest` is set, must be of one of the following types:
  // - string, plain object, ArrayBuffer, ArrayBufferView, URLSearchParams
  // - Browser only: FormData, File, Blob
  // - Node only: Stream, Buffer
  data: {
    firstName: 'Fred'
  }

所以尝试传递数据

let res = await usedClient.get(api1endpoints[1],{
  params: {
    data: res
  }
})

我已经测试了端点,它仅在响应类型为“text”或“stream”时才起作用

关于javascript - 向 Fluxpoint api 发送请求时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60779437/

相关文章:

javascript - Angular 中的路由

javascript - TypeError : ctx. onCreditChange 不是一个函数 Angular

node.js - Nodejs/Puppeteer - 如何使用 page.evaluate

node.js - 推送对象数组 Mongoose NodeJS

node.js - 添加服务的自定义端点 ( Feathersjs )

javascript - 函数/对象字面量之前的问号意味着

javascript setter 和 getter

Node.js + socket.io 返回事件数据

api - Last.fm geo.getEvents 返回无效方法 - 此包中没有具有该名称的方法

android - 如何从 APK 文件中提取 API 调用?