How to get encoded data in node.js? - node.js

When I submit the html form getting post data in server side and when I console my data is displayed like hemant%40gmail.com this is my server side code:
if(req.url=="/user_login"){
var logindata='';
req.on('data',function(data){
logindata+=data;
});
req.on('end',function(){
console.log(logindata);
});
}

I don't know what u really want, but with JavaScript you can do:
logindata += decodeURI(data);
Hope u just need to decode your encoded string?!
// update
ok u have to use "decodeURIComponent(data)" to decode your string.
here is an example:
var test = '"user_email=hemant%40gmail.com&password=111'
var decode = decodeURIComponent(test).split("=")[1].split("&")[0]
console.log(decode);
note: this is an easy example, u have to parse to get your email out of the string (use regex).

Related

Node Server and base64

I am having some issues converting a payload received (req.body) to a correctly formatted base64 string.
The payload is received and looks like the below example. I know that it's encrypted coming in but I'm wondering if there is anything that could be happening node server side that makes it look like this, it seems malformed and not how it should be
body: '&R۽5�l|L�\u001b�\u0014햱����\u00020#��[cV[AD&P�\u0001��˯���\n' +
`#B軉�6Y¤�\u0010�l�\u0012"D�dʦ�nb�g���\u0017����߉�{�a\u000e�:��\u0014\u0005�4\u0018!��u\u001e��s!վ]�\u0011KɆ�<!\u001d��#a�Ӿǥ+\f�iWEź�����:^�Վߎ�NP�M�G�_x�}�b1W�t�\u000f?*�2N�s��\u0000\u0015\u001e��o��� |y.\u0004n�e��64z�eu3\u0007(��j�R�\u0001 jzO\u0012�IF\u0002��w_����%�\u001b\u0010��\u0010��5�\u0016�.1�\u0006�\f\u0014�$�|\u000e�E�5�����o�MΆA\u001a��\u0010���׽���-ܹ��\u0003�jV�0b\u0002�\u001f��\\^"\\���\u0000��%�̓B�TfI��3��2U���[#�ۍ�'bT�]�\u0007�������\u0016 �P��x?\u0014�ly*8\u00134�NR����<��\u0012^�"#�V���!\u0010=�\u0006�"r�c�a�/L���vq�<\u0015�\u0006H��\u0014�\u001f�m�~�Ֆ�\u0011>L+����Yw���٘�\u0007ur�&�i�B4\n` +
If I convert the payload to a base64 string then i get something like this (the + and / characters do not seem right)
var encryptedBytes = Buffer.from(req.body);
var encryptedStr = encryptedBytes.toString('base64');
console.log({encryptedStr});
{ encryptedStr: 'JlLbvTXvv71sfEzvv70b77+9fxTtlrHvv73vv73vv73vv70CMCPvv73vv71bY1ZbQUQmUO+/vQHvv73vv73Lr++/ve+/ve+/vQojQui7ie+/vTZZwqTvv70Q77+9bO+/vRIiRO+/vWTKpu+/vW5i77+9Z++/ve+/ve+/vRfvv73vv73vv73vv73fie+/vXvvv71hDu+/vTrvv73vv70UBe+/vTQYIe+/ve+/vXUe77+977+9cyHVvl3vv70RS8mG77+'}
If I compare this to a base64 string I grab from the request on an iOS device for example, these seem rather different, plus this base64 string below can be decrypted successfully, which implies the issue could be within node?
{ base64data: 'ZGRIUUJhR0dMc3BTVFdQSHppS3BZUDY1UmJWSkFmbnRpekg1a29nUnlFMGtZemExU0RwS1h0VHlNd1lHMnhRcEZiMjEzNEwwYXduNllHR1p0aU1HM3YzcWlyTnlSd2RWSmNHQldMRVVMWklWaGRpNzBNWHVPNkZaSnJBUWZ6YnBJbERESzBiTEpoUGVCS3ZiU1d2NnRIcktIb'}
So my question here, is there something i need to do node server side to correctly parse or translate the req.body ready to be converted into a base64 string?

Cheerio's text() returns � (U+FFFD) for some characters

I'm creating a website scraping API, and sometimes there are special characters, often in names, eg. "Jesús M.Vargas".
My API downloads website content using request library and passes page's body to cheerio like this: const body = cheerio.load(page);
Then, I try to find text on the page, I know it's always the first <b>: var copyright = body('b').eq(0).text().trim();
Next, my code adds everything (like title, copyright, etc) to an object ({}) using pieces of code like data["copyright"] = copyright;
The last thing is to return the object to user as JSON object:
app.get("/api/", (req, res) => {
res.setHeader('Content-Type', 'application/json');
// query parameters
const date = req.query.date;
.
. // get the webpage, find the data, add everything to an object
.
// get the data from `data` object and return it as JSON
var output = JSON.stringify(data);
res.send(output);
}
When the API returns text like in the example above, "Jes�s M.Vargas" is visible in the JSON file. Is there a way to fix these characters? For stringifying object I'm using vanilla JavaScript, my web framework is Express

Array as parameter in GET request in Postman

I have to send array of ids in GET request as paramenter.How can I test it in Postman(google chrome extension for API testing)?
The scenario is I have url, www.something.com/activity/poi_ids
poi_ids should conatain arrays of ids like [316,318]
At api side using express,
app.get('/activity/:poi_ids',function(req,res){
var poi_ids = req.params.poi_ids;
.......
.......
});
I have looked into it but it is only for post request
you can send them via query params ..
in your http query params assign all values to same variables like
GET activity?pid=12&pid=23&pid=34
and then inside your express get it like
var ids=req.query.pid; //[12,23,34]
It is unstructured text. If you want to "send an array" then you'll need to design some way to encode it and then write JavaScript to decode it.
For example:
GET /activity/316-318
and
var poi_ids = req.params.poi_ids.split("-");

NodeJS Express encodes the URL - how to decode

I'm using NodeJS with Express, and when I use foreign characters in the URL, they automatically get encoded.
How do I decode it back to the original string?
Before calling NodeJS, I escape characters.
So the string: אובמה
Becomes %u05D0%u05D5%u05D1%u05DE%u05D4
The entire URL now looks like: http://localhost:32323/?query=%u05D0%u05D5%u05D1%u05DE%u05D4
Now in my NodeJS, I get the escaped string %u05D0%u05D5%u05D1%u05DE%u05D4.
This is the relevant code:
var url_parts = url.parse(req.url, true);
var params = url_parts.query;
var query = params.query; // '%u05D0%u05D5%u05D1%u05DE%u05D4'
I've tried url and querystring libraries but nothing seems to fit my case.
querystring.unescape(query); // still '%u05D0%u05D5%u05D1%u05DE%u05D4'
Update 16/03/18
escape and unescape are deprecated.
Use:
encodeURIComponent('אובמה') // %D7%90%D7%95%D7%91%D7%9E%D7%94
decodeURIComponent('%D7%90%D7%95%D7%91%D7%9E%D7%94') // אובמה
Old answer
unescape('%u05D0%u05D5%u05D1%u05DE%u05D4') gives "אובמה"
Try:
var querystring = unescape(query);
You should use decodeURI() and encodeURI() to encode/decode a URL with foreign characters.
Usage:
var query = 'http://google.com';
query = encodeURI(query);
query = decodeURI(query); // http://google.com
Reference on MDN:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURI
Decoding query parameters from a URL
decodeURIComponent cannot be used directly to parse query parameters from a URL. It needs a bit of preparation.
function decodeQueryParam(p) {
return decodeURIComponent(p.replace(/\+/g, ' '));
}
console.log(decodeQueryParam('search+query%20%28correct%29'));
// 'search query (correct)'
SOURCE: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent#decoding_query_parameters_from_a_url
#**update may-2020**
# how to use an encoder/decoder on node js
### I am writing my answer due to I have noisy data I spend 4 hour to fixed
data email input = myemail#gmail.com
data URL input = /us/home
```
decodeURI function that only decodes a URL special character
email output =>myemail%40gmail.com
url output => %2Fus%2F
using decodeURIComponent
email output = > myemail#gmail.com
url output => /us/
```
here some clarification where you can use decodeURI and decodeURIComponent a fucntion

Parsing postdata value in nodejs

I have a post data in the format below
authInfo={"user":"1","password":"a"}
How do I get the key ie authInfo. I am stuck here! req.query did not work out. Any help will be much appreciated.
Data transmitted via POST could be found in req.body.
For your example:
req.body.authInfo
Also: You need a data parser enabled, otherwise the Post data will not be decoded. I assume you use express, so you would need app.use(express.bodyParser()).
var authInfo = {"user":"1","password":"a"}
var user = authInfo.user
var pass = authInfo.password
// user = 1 , pass = a

Resources