How to inject formdata (or files in general) #4479
-
Hi, thanks for the hard work! I try to test Fastify (with fastify multipart plugin). When I build the application and do a request (with insomnia/postman) it works fine. But I want to test the application with the Server:
I tried a lot for the test so this is one of my attempts:
If I not add Maybe someone with experience in testing with inject and files? thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hi, const formAutoContent = require('form-auto-content');
const form = formAutoContent({
firstname: 'Foo',
lastname: 'Bar',
file: fs.createReadStream(path.join(__dirname, 'assets', 'fastify.pdf'))
});
const res = await fastify.inject({
method: 'POST',
url: '/upload-single',
...form
}); |
Beta Was this translation helpful? Give feedback.
-
The behavior is correct and intended, as by the spec RFC2046, the It indicates the boundary or end of a Request's body. For instance, it is required to allow the server to understand where the stream's body ends. Now, why it works in As @leandroandrade suggested, the usage of the given library handles all the logic required as |
Beta Was this translation helpful? Give feedback.
The behavior is correct and intended, as by the spec RFC2046, the
boundary
property is mandatory. It is an arbitrary value added upon dispatch of a given request and should be attached at the end of theMIME
of themultipart/form-data
content type header.It indicates the boundary or end of a Request's body. For instance, it is required to allow the server to understand where the stream's body ends.
Now, why it works in
insomnia
? It is becauseinsomnia
does this calculation and handling for you; meanwhile,light-my-request
(the engine behindFastify#inject
) does not do this calculation nor handling of theboundary
delimiter.As @leandroandrade suggested, the usage of the given library han…