Hello,
I have a form like that :
<form method="post" action="/post">
@each(item in items)
<input name="items[{{item.id}}]">
@endeach
<button type="submit">submit</button>
</form>
And I want it to return me an object like this:
{ '1' : 0, '2': 0 }
I don't know why but if one of the item.id isn't over 21 it return me an array instead :
[ 0, 0 ]
And when an item.id is over or equal to 21 it's working as expected
I'm confused...
Have you tried Request Collection?
That's not what I want,
I don't have multiple input name. I just need an object of 'items'
Like I said it's working with
<input name="items[1]">
<input name="items[21]">
But not with:
<input name="items[1]">
<input name="items[2]">
This might sound silly, but what does this give you:
<input name="items['1']">
<input name="items['2']">
Unfortunately I try that already and it doesn't work either,
it return me an object but :
{ '\'1\'': '2', '\'2\'': '1' }
You can't use a number as an object key - https://stackoverflow.com/a/3633390/1868365
Yeah sorry my bad,
I made a mistake.
I want it to return me :
{ '1' : 0, '2': 0 }
And that's what happened if an item.id is over or equal to 21.
The problem isn't the type of the key (string or number)
Does it do that if the numbers aren't sequential? It might be worth digging into the https://github.com/adonisjs/adonis-bodyparser code to see what it does
It doesn't seem to have an impact on it.
I try multiple value of ids and it's only over or equal to 21 that it does work
ids => [ 1, 2 ] => not working
ids => [ 1, 12 ] => not working
ids => [ 17, 12 ] => not working
ids => [ 1, 21 ] => working
ids => [ 25, 2 ] => working
What about [21, 22] or [1, 11, 21]? I don't think the answer will help us, I'm just curious
No matter the number of key or which is over 21, it's working
It's to do with the qs module, see the note about a limit of 20 here - https://github.com/ljharb/qs#parsing-arrays
You can define qs module options in the config (config/bodyparser.js) file as follows.
form: {
queryString: {
plainObjects: true
}
}
Huum,
it still doesn't work with
form: {
queryString: {
plainObjects: true
}
}
Can you share the complete config file with me?
'use strict'
module.exports = {
/*
|--------------------------------------------------------------------------
| JSON Parser
|--------------------------------------------------------------------------
|
| Below settings are applied when request body contains JSON payload. If
| you want body parser to ignore JSON payload, then simply set `types`
| to an empty array.
*/
json: {
/*
|--------------------------------------------------------------------------
| limit
|--------------------------------------------------------------------------
|
| Defines the limit of JSON that can be sent by the client. If payload
| is over 1mb it will not be processed.
|
*/
limit: '1mb',
/*
|--------------------------------------------------------------------------
| strict
|--------------------------------------------------------------------------
|
| When `scrict` is set to true, body parser will only parse Arrays and
| Object. Otherwise everything parseable by `JSON.parse` is parsed.
|
*/
strict: true,
/*
|--------------------------------------------------------------------------
| types
|--------------------------------------------------------------------------
|
| Which content types are processed as JSON payloads. You are free to
| add your own types here, but the request body should be parseable
| by `JSON.parse` method.
|
*/
types: [
'application/json',
'application/json-patch+json',
'application/vnd.api+json',
'application/csp-report'
]
},
/*
|--------------------------------------------------------------------------
| Raw Parser
|--------------------------------------------------------------------------
|
|
|
*/
raw: {
types: [
'text/*'
]
},
/*
|--------------------------------------------------------------------------
| Form Parser
|--------------------------------------------------------------------------
|
|
|
*/
form: {
types: [
'application/x-www-form-urlencoded'
],
queryString: {
plainObjects: true
}
},
/*
|--------------------------------------------------------------------------
| Files Parser
|--------------------------------------------------------------------------
|
|
|
*/
files: {
types: [
'multipart/form-data'
],
/*
|--------------------------------------------------------------------------
| Max Size
|--------------------------------------------------------------------------
|
| Below value is the max size of all the files uploaded to the server. It
| is validated even before files have been processed and hard exception
| is thrown.
|
| Consider setting a reasonable value here, otherwise people may upload GB's
| of files which will keep your server busy.
|
| Also this value is considered when `autoProcess` is set to true.
|
*/
maxSize: '20mb',
/*
|--------------------------------------------------------------------------
| Auto Process
|--------------------------------------------------------------------------
|
| Whether or not to auto-process files. Since HTTP servers handle files via
| couple of specific endpoints. It is better to set this value off and
| manually process the files when required.
|
| This value can contain a boolean or an array of route patterns
| to be autoprocessed.
*/
autoProcess: true,
/*
|--------------------------------------------------------------------------
| Process Manually
|--------------------------------------------------------------------------
|
| The list of routes that should not process files and instead rely on
| manual process. This list should only contain routes when autoProcess
| is to true. Otherwise everything is processed manually.
|
*/
processManually: []
}
}
And the request you are sending is a form request and not multipart or request with content-type: json
I can give you the full request :
Request URL: http://127.0.0.1:3333/mercurial/recapitulatif
Request Method: POST
Status Code: 302 Found
Remote Address: 127.0.0.1:3333
Referrer Policy: no-referrer-when-downgrade
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7,la;q=0.6
Cache-Control: max-age=0
Connection: keep-alive
Content-Length: 3495
Content-Type: application/x-www-form-urlencoded
Cookie: _ga=GA1.1.1624363788.1517563545; _gid=GA1.1.1602847978.1524471334; adonis-remember-token=ca7cc592f943415d93dbcce7f99a11ea6wRQnJZoZm70gqPCO2Qu0ijbwMWjUkit12WueGrhsetYcAI4uQ%2Fo8Dad3lFOjQfKlKlQsbESbBli25YWooiqAvxsgGisYrDg2aNiBSkx6eMIOSYs9kR7vZMXyQ0uqDkz; adonis-session-values=d7cf50b4cd05c126727e986c1ec8c7eaNpzmJMVLs4TNZ5LYvI4KCfxLoMn97IF6NLAhjLLVRoGLnNmUts6UINTkJVp0nffka4WQ08Ggn%2FHJm5MvcAOYvSVO8OMpcmM9lN5I9cDTdZO1CTrgFUyR61zGOmnHKPxLsFQpFk81kTypWZqsqXrK51VdsAS0Q9N8FTni6fQ5EJl%2B2TO9uTEHoBPF%2F2PmeczSfYbboWGH7FfS4cxD4YtMvywMQqdU9TPOAdi6nNxTSK4%3D; adonis-session=42260c99ba5e998858491ccbfa9ef55cc11O3KTo8d4c%2Fc7nCucod4IaI6Xp1wLC43AcbFyXEZnBNgGXJaO8np5Vvpy1gzuBPBzQlo9W6LUhkS%2BpVZjoxxovvcRo4nfqUoFI5oSUpFqbaEyxyU%2FYiX%2BygMa01Ryx; XSRF-TOKEN=e134eab787f2e0722f055c11e4183f10XhYTMvOIvVwD5tlxE5Fso80MN7tkj94ZIe15U3BhN7GoaHxnml9NBBTuvQIEpMS95iCyVc49ntJiXiDniMyYPRpWk%2BveHkG8acc657CsOh8ALro7coIwFyayejmdVDC%2B; _gat_gtag_UA_46106373_1=1
Host: 127.0.0.1:3333
Origin: http://127.0.0.1:3333
Referer: http://127.0.0.1:3333/mercurial/commande
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36
@garciamarco33 Sorry of taking much time on this. The bug was the bodyparser was not even passing this setting to qs module.
I have fixed it here https://github.com/adonisjs/adonis-bodyparser/commit/5f566014b39c4f9ce26655a01b3f829cf976f00f
Can you pull the latest version of bodyparser ie 2.0.3 and the config will be different.
{
queryString: { parseArrays: false }
}
It's working great !
Thank you !
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Most helpful comment
It's to do with the qs module, see the note about a limit of 20 here - https://github.com/ljharb/qs#parsing-arrays