Parse Form Urlencoded
NodeJS
Medium
4 views
Problem Description
Parse x-www-form-urlencoded body into object and print JSON.
Input Format
stdin: body text.
Output Format
Print JSON.
Sample Test Case
Input:
name=meetcode&level=easy
Output:
{\"name\":\"meetcode\",\"level\":\"easy\"}
Constraints
Use URLSearchParams.
Official Solution
const fs = require('fs');
const body = fs.readFileSync(0, 'utf8').trim();
if (!body) process.exit(0);
const params = new URLSearchParams(body);
console.log(JSON.stringify(Object.fromEntries(params.entries())));
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!