Request: 表单中带有空数组的帖子会删除该属性

创建于 2017-08-15  ·  3评论  ·  资料来源: request/request

概括


看来发帖时的表格有某种形式的优化。 这对我不利,它从表单中删除了属性。
我正在尝试发送一个对象,该对象的属性包含一个可能为空的数组。
每次数组为空时,都会返回 MissingPropertyError,这是不正确的,因为空数组实际上持有值而不是根本没有属性。

最简单的重现示例

const request = require( 'request' )
const bodyParser = require('body-parser')
const app = require('express')()

app.use( bodyParser.json() )
app.use( bodyParser.urlencoded( { extended: true } ) )

app.post('/', function( req, res ){
    console.log( 'received:', req.body )
    res.send('ok')
})
app.listen( 53153 )

const req = {
    url: 'http://localhost:53153',
    method: 'POST',
    headers: {
        'Content-Type' : 'application/x-www-form-urlencoded'
    },
    form: {
        emptyArray: [],
        filledArray: [{something:[]}]
    }
}
console.log( 'sending:', req )
request( req , function( err, httpResponse, body ){
    process.exit(0)
})

预期行为




我希望收到发送对象,因为它是有效的 json

$ node index.js
sending: { url: 'http://localhost:53153',
  method: 'POST',
  headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
  form: { emptyArray: [], filledArray: [ { something: [] } ] }
received: { emptyArray: [], filledArray: [ { something: [] } ] }

当前行为



结果发现接收到的对象只是一个空对象,查看wireshark,发送的内容长度为0。

$ node index.js
sending: { url: 'http://localhost:53153',
  method: 'POST',
  headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
  form: { emptyArray: [], filledArray: [ { something: [] } ] }
received: {}

向数组添加 null 会改变行为:

$ node index.js
sending: { url: 'http://localhost:53153',
  method: 'POST',
  headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
  form: { emptyArray: [ null ], filledArray: [ { something: [ null ] } ] }
received: { emptyArray: [ '' ], filledArray: [ { something: [ '' ] } ] }

但是在数组中添加 undefined 不会改变行为:

$ node index.js
sending: { url: 'http://localhost:53153',
  method: 'POST',
  headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
  form: { emptyArray: [ undefined ], filledArray: [ { something: [ undefined ] } ] }
received: {}

可能的解决方案


语境


您的环境

| 软件| 版本
| ---------------- | -------
| 请求 | 2.81.0
| 节点 | 8.3.0
| 每日头条 | 5.1.0
| 操作系统 | Ubuntu 16.04.2 LTS

stale

最有用的评论

无法相信在这个项目上工作的人都没有对此有任何意见。 上面的答案无效,因为它不理解提到的用例。 当问题很明显是关于“application/x-www-form-urlencoded”时,为什么我们要为“application/json”提供样本。 这是一个仍在发生的问题,但遗憾的是没有得到重视。

在我的测试中,未定义和空数组将在结果中消失,任何“空”都将出现在 url-encoded-string 中。

一个json:
{ results: { arr1: [], arr2: undefined, arr3: null, foo: { arr4: [] } } }
将产生一个只有
"?results%5Barr3%5D="

所有3条评论

因此,JSON 对象似乎正在转换为字符串,并在发送之前被设置为请求的正文。 这是您指定 'form' 属性时的默认行为。 此库用于将 JSON 转换为字符串的方法会删除空数组。 您需要做的不是传入 'form' 属性,而是传入 'body' 属性和 JSON.stringify 将您的 JSON 对象转换为字符串。 例如:

注意:您可能必须弄乱标题并使用 application/json 作为内容类型。 我还没有尝试过看看什么是有效的。

{
    url: 'http://localhost:53153',
    method: 'POST',
    headers: { 'Content-Type': 'application/json'},
    body: JSON.stringify({ emptyArray: [], filledArray: [ { something: [] } ] })
}

此问题已自动标记为陈旧,因为它最近没有活动。 如果没有进一步的活动发生,它将被关闭。 感谢你的贡献。

无法相信在这个项目上工作的人都没有对此有任何意见。 上面的答案无效,因为它不理解提到的用例。 当问题很明显是关于“application/x-www-form-urlencoded”时,为什么我们要为“application/json”提供样本。 这是一个仍在发生的问题,但遗憾的是没有得到重视。

在我的测试中,未定义和空数组将在结果中消失,任何“空”都将出现在 url-encoded-string 中。

一个json:
{ results: { arr1: [], arr2: undefined, arr3: null, foo: { arr4: [] } } }
将产生一个只有
"?results%5Barr3%5D="

此页面是否有帮助?
0 / 5 - 0 等级