[英]Read timed out. error while sending a POST request to a node.js API
I have a node.js API as below to which I send a POST request from python as below,the issue am facing is if I remove the headers={"Content-Type": "application/json"}
the POST goes thorugh,if not i get a Read timed out. error
我有一个 node.js API 如下所示,我从 python 向其发送 POST 请求,如下所示,面临的问题是如果我删除
headers={"Content-Type": "application/json"}
not={"Content-Type":"我得到一个Read timed out. error
Read timed out. error
, can anyone provide guidance on how to fix this timeout error? Read timed out. error
,任何人都可以提供有关如何解决此超时错误的指导吗?
node.js endpoint node.js 端点
app.post("/api/bats_push",(req, res) => {
//console.log("Calling bats_push...")
const d = {
method: req.method,
headers: req.headers,
query: req.query,
body: ''
}
req.on('data', (c) => {
//console.log(c)
d.body = d.body + c
});
req.on('end', () => {
DATA.push(d);
res.end('Saved BATS job details');
//res.status(200).json({
//message: "Saved BATS job details",
//posts: req.body
//});
});
});
Python POST Python 开机自检
try:
json"},timeout=10.0)
r = requests.post(webhook_url,data=json_data.encode("utf8"),verify=False,headers={"Content-Type": "application/json"})
print "posted"
print(r.status_code, r.reason)
print r.url
print r.text
except Exception as e:
print (e)
Error:-错误:-
InsecureRequestWarning)
HTTPSConnectionPool(host='company.com', port=443): Read timed out. (read timeout=10.0)
according to requests why not use this:根据要求为什么不使用这个:
#replace this
r = requests.post(webhook_url,data=json_data.encode("utf8"),verify=False,headers={"Content-Type": "application/json"})
#by this...assuming that 'data' is a dict
r = requests.post(webhook, json=data, verify=False)
Looks like it could be something related to your SSL.看起来它可能与您的 SSL 有关。 Check if you get the same error sending a request to your localhost with the server running.
检查在服务器运行的情况下向本地主机发送请求是否出现相同的错误。
From your question,the key word is:从你的问题来看,关键词是:
if I remove the headers={"Content-Type": "application/json"} the POST goes thorugh.如果我删除 headers={"Content-Type": "application/json"} POST 会很顺利。
The reason may clear:it is a wrong way use about the header.原因可能很清楚:header 的使用方式错误。
Simpler say: the node.js app check the header before into the logic code.简单地说:node.js 应用程序在进入逻辑代码之前检查 header。
if do not send the header by ourself,the requests part use the default headers below:如果不自己发送 header,则请求部分使用以下默认标头:
{
'User-Agent': 'python-requests/2.22.0',
'Accept-Encoding': 'gzip, deflate',
'Accept': '*/*',
'Connection': 'keep-alive',
}
through the requests's code can print the default headers when post.通过请求的代码可以在发布时打印默认标题。
you just use the header您只需使用 header
{"Content-Type": "application/json"}
may resulting in the node.js app think the reqeust not legitimate(the word I do not know how to explain in English).可能导致 node.js 应用程序认为请求不合法(这个词我不知道如何用英文解释)。
If the node.js app is developed by yourself, you can try to find the frame's check after create a tcp connection and before the logic code,the way is to read the source code.如果node.js这个app是自己开发的,可以尝试在创建tcp连接之后,在逻辑代码之前找到frame的check,方法是阅读源码。
If the node.js app is not developed by yourself,try to change the header mixing the default header to find which header key checked by the node.js app. If the node.js app is not developed by yourself,try to change the header mixing the default header to find which header key checked by the node.js app.
But in my thought,It is import,about the node.js app's interface description:但在我看来,关于 node.js 应用程序的界面说明,这很重要:
we just use by interface description engouth,just need to know the error from the api header's check, which the description should show but not show to us?我们只是通过接口描述engouth使用,只需要从api header的检查中知道错误,描述应该显示但不显示给我们?
Hope to help you.希望能帮到你。
I seems that you are using express.js.我似乎您正在使用 express.js。 I believe that your problem is, that body is actually already parsed.
我相信你的问题是,那个身体实际上已经被解析了。 You can check it by reading
req.body
.您可以通过阅读
req.body
来检查它。 The situation is caused because express.js already read whole body (due to the content type) and trying to read body again will cause timeout (event data
and event end
are not emitted).这种情况是因为 express.js 已经读取了整个正文(由于内容类型)并且尝试再次读取正文会导致超时(未发出事件
data
和事件end
)。 There are several ways how to fix it.有几种方法可以解决它。
disable express.js body parser - or reconfigure it to ignore json禁用 express.js 正文解析器 - 或重新配置它以忽略 json
remove reading body code and use directly req.body
去掉阅读正文代码,直接使用
req.body
app.post("/api/bats_push",(req, res) => {
//console.log("Calling bats_push...")
const d = {
method: req.method,
headers: req.headers,
query: req.query,
body: req.body
}
DATA.push(d);
res.end('Saved BATS job details');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.