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

Top-level JSON arrays are interpreted as objects #760

Closed
Rush opened this issue Sep 25, 2021 · 3 comments · Fixed by #761
Closed

Top-level JSON arrays are interpreted as objects #760

Rush opened this issue Sep 25, 2021 · 3 comments · Fixed by #761
Assignees

Comments

@Rush
Copy link

Rush commented Sep 25, 2021

Support plan

  • which support plan is this issue covered by? Community
  • is this issue currently blocking your project? No
  • is this issue affecting a production system? No

Context

  • node version: 14.x
  • module (formidable) version: 1.2.2
  • environment (e.g. node, browser, native, OS): node

What are you trying to achieve or the steps to reproduce?

const http = require('http');
const formidable = require('formidable');

const server = http.createServer((req, res) => {
  const form = formidable({ multiples: true });

  form.parse(req, (err, fields, files) => {
    if (err) {
      res.writeHead(err.httpCode || 400, { 'Content-Type': 'text/plain' });
      res.end(String(err));
      return;
    }
    console.log('Got fields', fields);
    res.writeHead(200, { 'Content-Type': 'application/json' });
    res.end(JSON.stringify({ fields, files }, null, 2));
  });
});

server.listen(9090, async () => {
  const res = await require('node-fetch')('http://localhost:9090/post', { method: 'POST', headers: { "Content-Type": "application/json" }, body: `[{ "foo": "bar" } ]` });
  console.log('Res', await res.json());
  process.exit(0);
});

What was the result you got?

JSON top-level array was interpreted as an object with '0' as key.

» node index.js
Got fields { '0': { foo: 'bar' } }
Res { fields: { '0': { foo: 'bar' } }, files: {} }

What result did you expect?

Expected to receive an array. Arrays shouldn't be interpreted as objeccts.

Got fields [ { foo: 'bar' } ]
@Rush Rush added the bug label Sep 25, 2021
@GrosSacASac
Copy link
Contributor

Yes, also top level numbers and strings are not supported I think

@GrosSacASac GrosSacASac self-assigned this Sep 25, 2021
@Rush
Copy link
Author

Rush commented Sep 25, 2021

Not sure if numbers and strings are universally considered valid JSON but top-level arrays definitely are as per this SO answer https://stackoverflow.com/a/3833312 which links to respective RFCs.

@GrosSacASac
Copy link
Contributor

json.org also and every JS implementation

GrosSacASac added a commit that referenced this issue Sep 26, 2021
GrosSacASac added a commit that referenced this issue Sep 26, 2021
GrosSacASac added a commit that referenced this issue Sep 29, 2021
GrosSacASac added a commit that referenced this issue Oct 11, 2021
GrosSacASac added a commit that referenced this issue Oct 30, 2021
* chore: start version 3

* feat: Use ES modules (#727)

* feat: Url search params and remove multiple (#730)

* docs: Fixed the missing "=" in options.filter (#732)

* docs: fix example

* docs: specify what part actually is

* docs: fix types

* docs: add warning for progress event

* docs: finish removing multiples in examples

* docs: add example for express as middleware

* docs: example handle common internet errors

* feat: add corepack support

* fix: corepack support

* fix: fix examples comment

* fix: fix example comment

* feat: add firstValues, readBooleans helpers (#757)

* feat: add firstValues, readBooleans helpers

* feat: export types

* docs: document helpers

* docs: fix example

* fix: #760 (#761)

* fix: fix error when there is an error

* tests: fix some of the tests  (#763)

* chore: up supertest jest and nyc

* tests: convert to import , fix some tests

* test: make malformed boundary as per comment ?

* tests: refactor

* style: fix some prettier issues (#764)

* chore: publish as 3.x tag

* docs: add changelog link

* docs: typo

Co-authored-by: Hrushikesh Das <dashrushikesh1121@gmail.com>
Co-authored-by: Jimmy Wärting <jimmy@warting.se>
@GrosSacASac GrosSacASac mentioned this issue Oct 30, 2021
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants