【问题标题】:Posting Base64 with Axios cause [Errno 54] Connection reset by peer使用 Axios 发布 Base64 导致 [Errno 54] Connection reset by peer
【发布时间】:2021-03-07 18:49:27
【问题描述】:

我目前正在开发一个使用 VueJS 作为前端,使用 Django (Django Rest Framework) 作为后端的 Web 应用程序。

该应用程序的功能之一是通过邮件发送 pdf 发票。所以我能够使用“jspdf”库生成pdf。在 Django 方面,我创建了一个 API 端点以发送电子邮件(附上 pdf)。

所以逻辑是(告诉我这样做是否错误):

  • 在前端将输出 pdf 转换为 Base64,以便使用 Axios 将其发布到我的“sendmail”端点。
  • 后端解码Base64字符串,写一个临时pdf文件,附上发送邮件。

效果很好,我测试过,发布请求的状态为 200。我收到附有 pdf 的邮件...但在 django 方面,我收到“[Errno 54] Connection reset by peer”。

这是完整的错误:

[24/Nov/2020 21:50:53] "POST /api/sendmail/ HTTP/1.1" 200 0
--------------------------------------------
Exception happened during processing of request from ('127.0.0.1', 59267)
Traceback (most recent call last):
  File "/Users/jjj/anaconda3/lib/python3.6/socketserver.py", line 639, in process_request_thread
    self.finish_request(request, client_address)
  File "/Users/jjj/anaconda3/lib/python3.6/socketserver.py", line 361, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/Users/jjj/anaconda3/lib/python3.6/socketserver.py", line 696, in __init__
    self.handle()
  File "/Users/jjj/Documents/DEV/Environments/project1_env/lib/python3.6/site-packages/django/core/servers/basehttp.py", line 174, in handle
    self.handle_one_request()
  File "/Users/jjj/Documents/DEV/Environments/project1_env/lib/python3.6/site-packages/django/core/servers/basehttp.py", line 182, in handle_one_request
    self.raw_requestline = self.rfile.readline(65537)
  File "/Users/jjj/anaconda3/lib/python3.6/socket.py", line 586, in readinto
    return self._sock.recv_into(b)
ConnectionResetError: [Errno 54] Connection reset by peer

问题是我尝试使用 Postman 进行发布请求,并且它没有任何错误地工作......这让我发疯了。 所以我怀疑错误出在前面。

这是我的 vue 端代码:

sendInvoice() {
    var mailInfo = {
        email: this.currentOrder.email,
        pdf: printPDF(this.currentOrder, true)
    }

    this.postSendMail(mailInfo)
}

这是我的 axios 代码:

postSendMail(context, mailInfo) {
    return new Promise((resolve, reject) => { 
        getAPI.post('/sendmail/', mailInfo) 
            .then(() => {
                resolve()
            })
    }) 
},

你有什么想法...?提前致谢。

【问题讨论】:

标签: django vue.js axios


【解决方案1】:

好吧,我现在感觉很笨。我只花了 3 个小时试图理解错误。

原因是在我的 axios 实例中,我将超时设置为 1000...并且由于我的 Base64 pdf 字符串非常重,因此需要超过 1 秒。我刚刚将它设置为 5000,它现在可以正常工作了!

【讨论】:

    猜你喜欢
    • 2019-11-04
    • 2022-12-06
    • 2020-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-02
    • 2016-06-21
    • 1970-01-01
    相关资源
    最近更新 更多