Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Different array parse behaviour of x-www-form-urlencoded between ExpressJS and rails #178

Open
hongyuanlei opened this issue Nov 11, 2016 · 4 comments

Comments

@hongyuanlei
Copy link

I use content-type: application/x-www-form-urlencoded to send POST request to backend server. The post body as follow:

listing[pictures][files][][name]=9ADDD7B1-FCFF-4538-916A-1E63DFB43A72&
listing[pictures][files][][name]=EFFE67B2-5D41-4B74-A33E-5963F54FAEC7&
listing[pictures][files][][name]=BBC9CB38-FE01-4F1A-AE37-7ED71F4B2CB5

In backend server, if I use ruby on rails, the POST will be parsed as follow:

"pictures"=>{
    "files"=>[
        {"name"=>"9ADDD7B1-FCFF-4538-916A-1E63DFB43A72"}, 
        {"name"=>"EFFE67B2-5D41-4B74-A33E-5963F54FAEC7"}, 
        {"name"=>"BBC9CB38-FE01-4F1A-AE37-7ED71F4B2CB5"}
    ]
}

This result is what I expected to be.

But, If I use nodeJS express and body-parser:

// parse application/json
app.use(bodyParser.json());
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));

extended: true means that bodyParser will use qs to parse data.

The POST will be parsed as follow:

"pictures"=>{
    "files"=>[
        "name"=>[
            "9ADDD7B1-FCFF-4538-916A-1E63DFB43A72", 
            "EFFE67B2-5D41-4B74-A33E-5963F54FAEC7", 
            "BBC9CB38-FE01-4F1A-AE37-7ED71F4B2CB5"
        ]
    ]
}
@ljharb
Copy link
Owner

ljharb commented Nov 11, 2016

hm, that does seem wrong. What version of body-parser and thus qs are you using?

@hongyuanlei
Copy link
Author

The version of body-parser is "1.15.2", and the version of qs is "6.2.0".

@hongyuanlei
Copy link
Author

I install the qs of versions 6.3.0 and do the test as following:

➜  npm list -g --depth=0
├── babel-cli@6.16.0
├── dateformat@1.0.12
├── istanbul@0.4.5
├── karma-cli@1.0.1
├── lodash@4.16.4
├── newman@3.1.2
├── nodemon@1.10.2
├── npm@3.9.5
├── qs@6.3.0
├── query-string@4.2.3
├── webpack@1.13.2
└── webpack-dev-server@1.15.1

➜  node
> var qs = require("qs");
undefined
> var listing_array = qs.parse('listing[pictures][files][][name]=9ADDD7B1-FCFF-4538-916A-1E63DFB43A72&listing[pictures][files][][name]=EFFE67B2-5D41-4B74-A33E-5963F54FAEC7&listing[pictures][files][][name]=BBC9CB38-FE01-4F1A-AE37-7ED71F4B2CB5');
undefined
> JSON.stringify(listing_array);
'{"listing":{"pictures":{"files":[{"name":["9ADDD7B1-FCFF-4538-916A-1E63DFB43A72","EFFE67B2-5D41-4B74-A33E-5963F54FAEC7","BBC9CB38-FE01-4F1A-AE37-7ED71F4B2CB5"]}]}}}'
>

The result still not what I wanted.

@ljharb
Copy link
Owner

ljharb commented Nov 11, 2016

A query string of 'listing[pictures][files][0][name]=9ADDD7B1-FCFF-4538-916A-1E63DFB43A72&listing[pictures][files][1][name]=EFFE67B2-5D41-4B74-A33E-5963F54FAEC7&listing[pictures][files][2][name]=BBC9CB38-FE01-4F1A-AE37-7ED71F4B2CB5' does parse how you want - but I agree this is strange.

I believe this might be related to #122 and perhaps also #123, but I'm going to leave this open also.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants