简体   繁体   English

错误:尚未准备好解析。 缺少内容类型?

[英]Error: Not ready to parse. Missing Content-Type?

I'm driving a node.js/express.js app with curl. 我正在用curl驱动一个node.js / express.js应用程序。 The curl command: curl -X PUT -i -T LICENSE.txt localhost:9981/v1/test/bucket/some-object -s curl命令: curl -X PUT -i -T LICENSE.txt localhost:9981/v1/test/bucket/some-object -s

errors, saying content-type should be present. 错误,提示应显示内容类型。 Content-type of 'text/plain' does not work. “文本/纯文本”的内容类型无效。 In fact, the only content-type that works seems to be: Content-Type: multipart/form-data; boundary=---something 实际上,唯一起作用的内容类型似乎是: Content-Type: multipart/form-data; boundary=---something Content-Type: multipart/form-data; boundary=---something

However! 然而! What should be the boundary, if I'm only putting some text file? 如果我只放置一些文本文件,边界应该是什么? The curl command hangs because, I assume, the boundary cannot be made up. curl命令挂起,因为我认为无法弥补边界。

Now, this curl command works: curl localhost:9981/v1/test/container/some-object -X PUT -F "file=@/opt/nedge/README.md" But this is not quite what I want, because I don't want to specify file=@ 现在,此curl命令起作用: curl localhost:9981/v1/test/container/some-object -X PUT -F "file=@/opt/nedge/README.md"但这不是我想要的,因为我不想指定file=@

And the last piece of the question is, the node.js code that is being run. 问题的最后一部分是正在运行的node.js代码。 It is the example busboy, the first example from their github: https://github.com/piousbox/busboy 这是示例busboy,这是他们github上的第一个示例: https : //github.com/piousbox/busboy

My question is: either how to specify the correct boundary with curl, or how to make busboy (or node.js streams overall) accept content-type other than multipart/form-data . 我的问题是:如何使用curl指定正确的边界,或者如何使busboy(或总的来说,node.js流)接受除multipart/form-data之外的内容类型。

Busboy only processes multipart/form-data (generally when at least one of the fields is a file) or application/x-www-form-urlencoded requests (no files). Busboy仅处理multipart / form-data(通常在至少一个字段是文件的情况下)或application / x-www-form-urlencoded请求(无文件)。 If you need to process a 'text/plain' or other type of request, then just add your own middleware before your regular form processing middleware. 如果您需要处理“文本/纯文本”或其他类型的请求,则只需在常规表单处理中间件之前添加您自己的中间件即可。 Something like this perhaps: 大概是这样的:

// ....

app.use(function(req, res, next) {
  if (!req.headers['content-type'])
    return next(new Error('Bad request'));
  if (req.headers['content-type'].indexOf('text/plain') === 0) {
    // read from `req` here with req.read() or req.on('data', ...), etc.
  } else
    next();
});

// ....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM