Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
form-urlencoded
Advanced tools
The form-urlencoded npm package is used to encode and decode URL-encoded form data. It is particularly useful for working with HTTP requests and responses where data needs to be sent or received in the application/x-www-form-urlencoded format.
Encoding an Object to URL-encoded String
This feature allows you to convert a JavaScript object into a URL-encoded string, which is useful for sending data in HTTP POST requests.
const formurlencoded = require('form-urlencoded');
const data = { name: 'John Doe', age: 30, city: 'New York' };
const encoded = formurlencoded(data);
console.log(encoded); // Output: 'name=John%20Doe&age=30&city=New%20York'
Decoding a URL-encoded String to an Object
This feature allows you to convert a URL-encoded string back into a JavaScript object, which is useful for processing data received in HTTP POST requests.
const formurlencoded = require('form-urlencoded');
const encoded = 'name=John%20Doe&age=30&city=New%20York';
const decoded = formurlencoded.parse(encoded);
console.log(decoded); // Output: { name: 'John Doe', age: '30', city: 'New York' }
The qs package is a query string parser and stringifier with support for nested objects. It provides more advanced features compared to form-urlencoded, such as handling nested objects and arrays.
The querystring package is a Node.js core module that provides utilities for parsing and formatting URL query strings. It is similar to form-urlencoded but is built into Node.js and may not have as many features.
The urlencode package is a simple utility for URL encoding and decoding. It is similar to form-urlencoded but focuses solely on encoding and decoding without additional features.
(c)Bumblehead, JBlashill
Returns 'x-www-form-urlencoded' string data, an encoding often used when an HTML form is submitted. Form data is serialised in this format and sent to a server.
import formurlencoded from 'form-urlencoded';
// or:
// var formurlencoded = require('form-urlencoded').default;
const obj = {
str : 'val',
num : 0,
arr : [3, {prop : false}, 1, null, 6],
obj : {prop1 : null, prop2 : ['elem']}
};
console.log(formurlencoded(obj));
// str=val&num=0&arr%5B0%5D=3&arr%5B1%5D%5Bprop%5D=false
// &arr%5B2%5D=1&arr%5B3%5D=null&arr%5B4%5D=6&obj%5Bprop
// 1%5D=null&obj%5Bprop2%5D%5B0%5D=elem
console.log(formurlencoded(obj, {
ignorenull : true,
skipIndex : true,
sorted : true
}));
// arr%5B%5D=3&arr%5B%5D%5Bprop%5D=false&arr%5B%5D=1&arr
// %5B%5D=6&num=0&obj%5Bprop2%5D%5B%5D=elem&str=val
console.log(formurlencoded(obj, {
ignorenull : true,
useDot : true,
skipIndex : true,
skipBracket : true
}));
// str=val&num=0&arr=3&arr.prop=false&arr=1&arr=6&obj.pr
// op2=elem
changelog
(The MIT License)
Copyright (c) Bumblehead chris@bumblehead.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
Return an object as an 'x-www-form-urlencoded' string
The npm package form-urlencoded receives a total of 193,581 weekly downloads. As such, form-urlencoded popularity was classified as popular.
We found that form-urlencoded demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.