node.js - 使用 request 发出 POST HTTP 请求不会返回 request.Response 用于回调
问题描述
我不知道为什么,但是如果我使用 POST 响应,request(options,callback)
没有request.Response
对象传递callback
给X-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()
解决方案
推荐阅读
- java - 使用 PDFBox 将存储为字节数组的图像添加到 PDF
- topic-modeling - 我们可以恢复 MALLET 模型的训练吗?
- jmeter - 在 JMeter 中使用 Blazemeter HTTP2 采样器时出现 TimeoutException
- c# - 为什么 Visual Studio 将所有新类标记为
在我的 csproj 文件中? - python - 如何将 python 程序转换为 Azure 函数?
- java - 我可以用 Java 替换 SQL*Loader 代码来读取 ctl 文件以上传重文件而不影响性能吗?
- javascript - 在本机反应中修剪视频
- python - 用于 Python 中竞争性编程的快速 I/O
- javascript - 用js获取mp3文件频率数据
- python - 将上传的文件连接到 Django 中的帖子