Using request to make a POST HTTP request does not return a request.Response for the callback

39 Views Asked by At

I don't know why, but if I do a POST response using request(options,callback), no request.Response object is passed to the callback, I need the response object to check for the X-CSRF-TOKEN header if it exists or not, and if it does exist I need to set it to a dictionary http_header and if the statusCode is 403 then I would resend the request and resolve the 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()
0

There are 0 best solutions below