expressjs / expressjs/body-parser

json middleware does not work on content types with a `+`

Open
#519 9 comments 0 reactions 0 assignees View on GitHub
discuss enhancement ideas
Dominant language
JavaScript
Stars
5.5k
Forks
767
Avg merge
13h 53m
Merged PRs (30d)
10

Description

The json middleware does not parse results as json if the content type is anything but `application/json`, I would expect it to also work on content types like `application/ld+json`. For instance:

```ts
import express from 'express';
const app = express();
app.use(express.json());

app.post('/', async (req, res) => {
console.log("Data posted", await req.body);
});

app.listen(3000, () => {
console.log('Listening on port 3000');
fetch('http://localhost:3000', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
message: 'Hello world',
}),
});
});
```

returns

```
Listening on port 3000
Data posted { message: 'Hello world' }
```

On the other hand

```ts
import express from 'express';
const app = express();
app.use(express.json());

app.post('/', async (req, res) => {
console.log("Data posted", await req.body);
});

app.listen(3000, () => {
console.log('Listening on port 3000');
fetch('http://localhost:3000', {
method: 'POST',
headers: {
'Content-Type': 'application/ld+json',
},
body: JSON.stringify({
message: 'Hello world',
}),
});
});
```

returns

```
Listening on port 3000
Data posted {}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.