Improve FormData logging (or documentation)
See original GitHub issueDescribe the problem
I’m always frustrated when working with await event.request.formData() because it is not easily log-able.
export async function post(event) {
const body = await event.request.formData()
console.log('body', body)
console.log('body', body.entries())
console.log('body', body.keys())
console.log('body', body.values())
...
All those simple measures during development to see if form data arrives and what values are contained do not work with current SvelteKit:
body FormData {}
body Object [Generator] {}
body Object [Generator] {}
body Object [Generator] {}
Especially the fact that a simple console.log(body) yields no sense-making output is very irritating to me. As a SvelteKit newbie it took me an hour to understand that it’s not a problem with the data transfer but only with the way I want to display the data.
Describe the proposed solution
const body = await event.request.formData();
console.log(body);
should show body content, formatting doesn’t really matter.
[ [ "name": "Rich Harris" ], [ "hobbies", "svelte" ], [ "hobbies": "journalism" ] ]
Alternatives considered
Add an example to the endpoint body parsing documentation.
const body = await event.request.formData();
const entries = [...body.entries()];
// [ [ "name": "Rich Harris" ], [ "hobbies", "svelte" ], [ "hobbies": "journalism" ] ]
Importance
would make my life easier
Additional Information
No response
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Using FormData Objects - Web APIs | MDN
The FormData object lets you compile a set of key/value pairs to send using XMLHttpRequest. It is primarily intended for use in sending...
Read more >How to inspect FormData? - javascript
Longer answer. Others suggest logging each entry of fd.entries() , but the console.log can also take multiple arguments console ...
Read more >FormData
FormData objects are used to capture HTML form and submit it using fetch or another network method. We can either create new FormData(form)...
Read more >FormData For JavaScript. How form data is sent, and files too!
You often learn a lot when reading API documentation, and in this case I ... log => data type = object FormData {...
Read more >How to Inspect the JavaScript FormData Instance?
We create the formData object with the FormData constructor. ... In the for-of loop, we destructure the key-value pairs and log them with ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Yeah, we definitely don’t want to be monkey-patching or mucking about with prototypes. The MDN FormData documentation also isn’t much help in this regard though.
Logging is pretty straightforward once you know how…
…and the same thing applies to lots of similarly-shaped objects (
Map,URLSearchParams, etc).I’m always a little torn in situations like this. If Svelte/SvelteKit docs need to show how to use various web APIs (including things like
RequestandResponse) then they’ll quickly get bloated. At the same time, many developers will encounter those APIs for the first time while using Svelte, and it can be hard to find relevant/high quality documentation elsewhere. There’s no right answer.Thanks for clarification, will close issue.