错误:运行帖子时错误的标题检查

编程入门 行业动态 更新时间:2024-10-09 22:21:22
本文介绍了错误:运行帖子时错误的标题检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我需要从休息调用中获取zip(对于模拟我使用postman与二进制选项发布,并添加一个zip文件与文件夹和html文件),在模拟期间我想要获取数据与快递和提取的zip并放在C盘下的一些文件夹中。 当我运行以下程序(这是我尝试过的所有代码),但是我收到错误

事件.js:85 呃//未处理的'错误'事件 ^错误:不正确的头部检查在Zlib._handle.onerror(zlib.js:366:17)

var express = require('express'), fs = require('fs'), zlib = zlib'), app = express(); app.post('/',function(req,res){ var writeStream = fs.createWriteStream('C:// myFolder',{flags:'w'}) ; req.pipe(zlib.createInflate())。pipe(writeStream); }); var server = app.listen(3000,function(){ console.log(Running on port+ 3000)} )

在邮递员标题中我添加了以下

content-Type ---->应用程序/ zip

我应该如何克服这个问题并保存zip? 还有其他推荐(zlib)库来提取和保存zip?

解决方案

zlib 旨在提取gzip压缩或放空的数据,而不是.ZIP文件。

您可以使用 node-unzip 模块:

var unzip = require('unzip'); ... app.post('/',function(req,res){ var extractor = unzip.Extract({path:'C:// myFolder'}) on('close',function(){ res.sendStatus(200); })。on('error',function(err){ res.sendStatus(500); }); req.pipe(extractor); });

如果Postman无法处理这样的上传(如评论中所建议的),您可以使用cURL:

$ curl -XPOST localhost:3000 --data-binary @ test.zip

I need to get zip from rest call (for simulation I use postman with binary option for post and add a little zip file with folder and html file),during the simulation I want to get the data with express and extract the zip and put in some folder under C drive. Currently when I run the following program(this is all the code which i've tried) but im getting error

events.js:85 throw er; // Unhandled 'error' event ^ Error: incorrect header check at Zlib._handle.onerror (zlib.js:366:17)

var express = require('express'), fs = require('fs'), zlib = require('zlib'), app = express(); app.post('/', function (req, res) { var writeStream = fs.createWriteStream('C://myFolder', {flags: 'w'}); req.pipe(zlib.createInflate()).pipe(writeStream); }); var server = app.listen(3000, function () { console.log("Running on port" + 3000) } )

in postman header i've added the following

content-Type ----> application/zip

How should I overcome this issue and save the zip ? there is other recommended (zlib)library to get extract and save zip?

解决方案

zlib is meant to extract gzipped or deflated data, not .ZIP files.

You can use the node-unzip module for those:

var unzip = require('unzip'); ... app.post('/', function(req, res) { var extractor = unzip.Extract({ path : 'C://myFolder' }).on('close', function() { res.sendStatus(200); }).on('error', function(err) { res.sendStatus(500); }); req.pipe(extractor); });

If Postman can't handle uploads like this (as suggested in the comments), you can test using cURL:

$ curl -XPOST localhost:3000 --data-binary @test.zip

更多推荐

错误:运行帖子时错误的标题检查

本文发布于:2023-10-10 18:36:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1479395.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   标题   帖子

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!