【问题标题】:Error while sending a request to fluxpoint api向fluxpoint api发送请求时出错
【发布时间】:2020-07-01 21:38:57
【问题描述】:

又是我……很抱歉一天问这么多次,但我真的是个白痴。

所以基本上我正在尝试使用以下代码向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 发送给我的错误:

Failed to parse json, The input does not contain any JSON tokens. Excepted the input to start with a valid JSON token, when isFinalBlock is true. Path: $ | LineNumber: 0 | BytePositionInLine: 0.

我什么都试过了,但是 JSON.parse(my data) 给我发了Unexcepted token o in JSON at position 1

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

【问题讨论】:

    标签: javascript node.js api request


    【解决方案1】:

    您似乎正在解析原始 json。它会引发错误

    JSON.parse 以字符串为参数。

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

    并且从官方文档中,您不能在获取请求中使用数据。

    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
      }
    })
    

    我已经测试了它只有在 responseType 是 'text' 或 'stream' 时才有效的端点

    【讨论】:

    • 即使我这样做,API 也会回答我 "{ "status": "error", "success": false, "code": 400, "message": "Failed to parse json , 输入不包含任何 JSON 标记。当 isFinalBlock 为真时,预期输入以有效的 JSON 令牌开头。路径:$ |行号:0 | BytePositionInLine: 0。" }"...
    • 您好,您可以将console.log(res) 的输出添加到问题中吗?
    • 是的,我会这样做
    • 是不是nodejs环境下的api??
    • 如果我的回答对您有帮助,请点赞并将其标记为正确。我在这篇文章中花了一些时间。
    猜你喜欢
    • 2021-07-31
    • 1970-01-01
    • 2019-09-09
    • 1970-01-01
    • 2019-08-08
    • 2014-02-12
    • 1970-01-01
    • 2017-02-07
    • 1970-01-01
    相关资源
    最近更新 更多