fastify / fastify/fastify-multipart

`attachFieldsToBody` makes `request.file()` return undefined.

Open
#472 7 comments 0 reactions 0 assignees View on GitHub
discussion
Dominant language
JavaScript
Stars
539
Forks
126
PR merge metrics
No merged PRs in 30d

Description

### Prerequisites

- [X] I have written a descriptive issue title
- [X] I have searched existing issues to ensure the bug has not already been reported

### Fastify version

4.21.0

### Plugin version

7.7.3

### Node.js version

20.3.1

### Operating system

Windows

### Operating system version (i.e. 20.04, 11.3, 10)

10.0.19044 Build 19044

### Description

I'm not entirely sure if this is an intentional change, but it's certainly one that I don't personally agree with. As far as I'm aware and have explored, there is no possible way to get the name of the file as `request.body.(field where the file should be)` provides only the buffer and nothing else.

### Steps to Reproduce

```ts
import fastify from "fastify";
import "@fastify/multipart";

const instance = fastify({ logger: true });
instance.register(require("@fastify/multipart"), {
attachFieldsToBody: "keyValues",
});

instance.route({
method: "POST",
url: `/files/`,
handler: async (request, reply) => {
const file = await request.file();
console.log(file?.filename || "no file"); // returns "no file"

return {
code: 200,
message: "ok"
}
}
})

instance.listen({ port: 8090 }, (err, address) => {
if (err) {
instance.log.error(err);
process.exit(1);
}
instance.log.info(`Server listening on ${address}`);
})
```

### Expected Behavior

Instead of returning `no file`, it should return the data of the sent file without requiring the usage of `request.body.(field where the file should be)`.

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.