Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL component encoding in Node.js

I want to send http request using node.js. I do:

http = require('http');

var options = {
    host: 'www.mainsms.ru',
    path: '/api/mainsms/message/send?project='+project+'&sender='+sender+'&message='+message+'&recipients='+from+'&sign='+sign
    };

    http.get(options, function(res) {
    console.log('STATUS: ' + res.statusCode);
    console.log('HEADERS: ' + JSON.stringify(res.headers));
    }).on('error', function(e) {
    console.log('ERROR: ' + e.message);
    });

When my path like this:

/api/mainsms/message/send?project=geoMessage&sender=gis&message=tester_response&recipients=79089145***&sign=2c4135e0f84d2c535846db17b1cec3c6

Its work. But when message parameter contains any spaces for example tester response all broke. And in console i see that http use this url:

  /api/mainsms/message/send?project=geoMessage&sender=gis&message=tester

How to send spaces. Or i just can't use spaces in url?

like image 516
Kliver Max Avatar asked Sep 29 '13 10:09

Kliver Max


People also ask

What is URL encoding in node JS?

URL encoding is commonly used to avoid cross-site scripting (XSS) attacks by encoding special characters in a URL. It converts a string into a valid URL format that makes the transmitted data more reliable and secure.

How do I URL encode a node js string?

encodeURIComponent. To encode special characters in URI components, you should use the encodeURIComponent() method. This method is suitable for encoding URL components such as query string parameters and not the complete URL.

Should I use encodeURI or encodeURIComponent?

encodeURIComponent should be used to encode a URI Component - a string that is supposed to be part of a URL. encodeURI should be used to encode a URI or an existing URL.

How do you encode a character in a URL?

URL encoding replaces unsafe ASCII characters with a "%" followed by two hexadecimal digits. URLs cannot contain spaces. URL encoding normally replaces a space with a plus (+) sign or with %20.


3 Answers

What you are looking for is called URL component encoding.

path: '/api/mainsms/message/send?project=' + project + 
'&sender=' + sender + 
'&message=' + message +
'&recipients=' + from + 
'&sign=' + sign

has to be changed to

path: '/api/mainsms/message/send?project=' + encodeURIComponent(project) +
'&sender=' + encodeURIComponent(sender) +
'&message=' + encodeURIComponent(message) + 
'&recipients='+encodeURIComponent(from) +
'&sign=' + encodeURIComponent(sign)

Note:

There are two functions available. encodeURI and encodeURIComponent. You need to use encodeURI when you have to encode the entire URL and encodeURIComponent when the query string parameters have to be encoded, like in this case. Please read this answer for extensive explanation.

like image 169
thefourtheye Avatar answered Oct 06 '22 17:10

thefourtheye


The question is for Node.js. encodeURIcomponent is not defined in Node.js. Use the querystring.escape() method instead.

var qs = require('querystring');
qs.escape(stringToBeEscaped);
like image 29
David Welborn Avatar answered Oct 06 '22 16:10

David Welborn


The best way is to use the native module QueryString :

var qs = require('querystring');
console.log(qs.escape('Hello $ é " \' & ='));
// 'Hello%20%24%20%C3%A9%20%22%20\'%20%26%20%3D'

This is a native module, so you don't have to npm install anything.

like image 22
Julien CROUZET Avatar answered Oct 06 '22 17:10

Julien CROUZET