简体   繁体   English

读取超时。 向 node.js API 发送 POST 请求时出错

[英]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.有几种方法可以解决它。

  1. disable express.js body parser - or reconfigure it to ignore json禁用 express.js 正文解析器 - 或重新配置它以忽略 json

  2. 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.

相关问题 读取超时。 (读取超时=无)通过 simple_salesforce API 在 Python 中插入记录时 - Read timed out. (read timeout=None) when inserting records through simple_salesforce API in Python ElasticSearch 超时错误:ReadTimeoutError(HTTPConnectionPool(host='localhost', port=9200): 读取超时。(读取超时=60)) - ElasticSearch Timeout Error: ReadTimeoutError(HTTPConnectionPool(host='localhost', port=9200): Read timed out. (read timeout=60)) Newspaper3k API 文章下载()失败,HTTPSConnectionPool 端口=443 读取超时。 URL 上的(读取超时 = 7) - Newspaper3k API Article download() failed with HTTPSConnectionPool port=443 Read timed out. (read timeout=7) on URL 使TCP连接超时:110:连接超时。 在AWS上同时使用scrapy吗? - Getting TCP connection timed out: 110: Connection timed out. on AWS while using scrapy? 导入 Python API 时 node.js 中出现“找不到模块”错误 - "Module not found" error in node.js while importing Python API 由-ReadTimeoutError(HTTPConnectionPool(host = u'localhost',port = 9200)引起的ConnectionTimeout:读取超时。(读取超时= 10) - ConnectionTimeout caused by - ReadTimeoutError(HTTPConnectionPool(host=u'localhost', port=9200): Read timed out. (read timeout=10)) ReadTimeout: HTTPSConnectionPool(host='', port=443): 读取超时。 (读取超时=10) - ReadTimeout: HTTPSConnectionPool(host='', port=443): Read timed out. (read timeout=10) 将循环结构转换为 JSON,同时将 json 从 Flask 发送到 Node.js 时出错 - Error Converting circular structure to JSON, while sending json from Flask to Node.js 发送带有数据的POST请求时出错 - Error while sending POST request with data pycorenlp:“CoreNLP请求超时。你的文件可能太长了“ - pycorenlp: “CoreNLP request timed out. Your document may be too long”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM