首页 > 解决方案 > 使用 request 发出 POST HTTP 请求不会返回 request.Response 用于回调

问题描述

我不知道为什么,但是如果我使用 POST 响应,request(options,callback)没有request.Response对象传递callbackX-CSRF-TOKEN字典http_header,如果statusCode是 403,那么我将重新发送请求并解决 Promise。

var http_header = {
    "Content-Type": "application/json",
    "Cookie": ".ROBLOSECURITY="+ROBLOSECURITY
}

function MakeRbxReq(http_method, url, payload) {
    let jsonpayload
    try {
        if (payload == undefined) {return}
        jsonpayload = JSON.stringify(payload)
    } finally {}

    var options = {
        uri: "http://" + url,
        body: jsonpayload || "",
        methpd: http_method,
        headers: http_header
    }
    
    return new Promise(resolve => {
        request(options, (_,response) => {
            if (http_method.toUpperCase() == "POST" || http_method.toUpperCase() == "DELETE" || http_method.toUpperCase() == "PUT" || http_method.toUpperCase() == "PATCH") {
                console.log("RES POST: "+response) // This would be undefined somehow
                // The rest of the code below will error
                if (response.headers["X-CSRF-TOKEN"] != undefined) {
                    http_header["X-CSRF-TOKEN"] = response.headers["X-CSRF-TOKEN"]
                    options.headers = http_header
                    if (response.statusCode == 403) {
                        request(options, (_,res) => {
                            resolve({statusCode: res.statusCode, body: res.body})
                        })
                        return
                    }
                }
            }
            resolve({statusCode: response.statusCode, body: response.body})
            return
        })
    })
}

async function t() {
    await MakeRbxReq("POST","https://economy.roblox.com/v1/purchases/products/123",{})
}

t()

标签: node.jsnode-request

解决方案


推荐阅读