What is a good replacement for new Buffer("my string", "binary") - node.js

According to the node docs the "binary" encoding will be deprecated in future versions.
However I found that my code only works if I create my buffer like this:
var buffer = new Buffer("Special chars like ñ and backspace", "binary");
What is the right way to achieve the same thing?

Fixed it simply by passing the encoding parameter to http.write, like this:
var http = require("http");
var httpReq = http.request(options, callback);
httpReq.write("some string with special characters", "binary");
There are no deprecations in the docs about the write method AFAIK.

Related

nodejs handling arraybuffers

suppose I make a multipart, application/octet-stream request with responseType as 'arraybuffer'...suppose I receive this in nodejs and I try to write the response into a file. How can I handle this such that I don't corrupt the contents?
My current approach is something like this
var req = var req = restler.post(url, opts)
.on('data', function (data){
console.log('receiving data...');
console.log(data);
}).on('complete', function (data) {
var buff = new Buffer(data) //this is prolly incorrect, but I can't figure this out at all
fs.writeFile(file_name, buff.toString('binary'), function(err){
console.log('done!')
});
Here I write the contents into filename.
Suppose I fetch a microsoft word file...fetching it only leads me to a corrupt file. Also using restler package for this
According to the restler documentation, you can set decoding: 'buffer' in your opts and it will keep the binary data intact as a Buffer instead of the default utf8-encoded string. From there it's just a matter of passing the buffer directly to fs.writeFile() without calling buffer.toString().

How to convert file to buffer in node.js?

I am writing a sails.js app. I am writing an API to accept a file and encrypt it.
var file = req.body('myFile');
var fileBuffer = convertToBuffer(file);
How do I convert a file to buffer?
That looks like you've got a string which represents the body of your file.
You just have to make a new buffer with it.
var fileBuffer = Buffer.from(file)
If your encoding is NOT utf8 you can specify an alternate encoding as a second optional argument.
var fileBuffer = Buffer.from(file, 'base64')
If the file is actually on disk, this is even easier, since, by default, the fs.readFile operation returns a buffer.
fs.readFile(file, function(err, buffer){})
If you're in a real old version of node Buffer.from doesn't exist and you have to use a very memory-unsafe new constructor. Please consider upgrading your node instance to support Buffer.from

TypeError: Request path contains unescaped characters, how can I fix this

/*Making http request to the api (Git hub)
create request
parse responce
wrap in a function
*/
var https = require("https");
var username = 'lynndor';
//CREATING AN OBJECT
var options = {
host: 'api.github.com',
path: ' /users/'+ username +'/repos',
method: 'GET'
};
var request = https.request(options, function(responce){
var body = ''
responce.on("data", function(chunk){
body += chunk.toString('utf8')
});
responce.on("end", function(){
console.log("Body", body);
});
});
request.end();
Im trying to create a request to the git hub api, the aim is to get the list repository for the specified you, but i keep getting the above mentioned error, please help
for other situation can be helpful
JavaScript encodeURI() Function
var uri = "my test.asp?name=ståle&car=saab";
var res = encodeURI(uri);
Your "path" variable contains space
path: ' /users/'+ username +'/repos',
Instead it should be
path: '/users/'+ username +'/repos',
Use encodeURIComponent() to encode uri
and decodeURIComponent() to decode uri
Its because there are reserved characters in your uri. You will need to encode uri using inbuilt javascript function encodeURIComponent()
var options = {
host: 'api.github.com',
path: encodeURIComponent('/users/'+ username +'/repos'),
method: 'GET'
};
to decode encoded uri component you can use decodeURIComponent(url)
Typically, you do not want to use encodeURI() directly. Instead, use fixedEncodeURI(). To quote MDN encodeURI() Documentation...
If one wishes to follow the more recent RFC3986 for URLs, which makes square brackets reserved (for IPv6) and thus not encoded when forming something which could be part of a URL (such as a host), the following code snippet may help:
function fixedEncodeURI(str) {
return encodeURI(str).replace(/%5B/g, '[').replace(/%5D/g, ']');
}
There is a similar issue with encodeURIComponent() (source: MDN encodeURIComponent() Documentation), as well as a similar fixedEncodeURIComponent() function. These should be used, rather than the actual encodeURI() or encodeURIComponent() function calls...
To be more stringent in adhering to RFC 3986 (which reserves !, ', (, ), and *), even though these characters have no formalized URI delimiting uses, the following can be safely used:
function fixedEncodeURIComponent(str) {
return encodeURIComponent(str).replace(/[!'()*]/g, function(c) {
return '%' + c.charCodeAt(0).toString(16);
});
}
I was getting this error while trying to hit Elasticsearch's API. For me, it was due to Chinese characters in the Document's Title (in the Request I was sending). Switching to all English characters fixed the issue.
Sometimes browser inspector uses abbreviation of long JSON object.
In my case, the data included unescaped characters such as '…' which should not be in http request.

Don't encode urls in querystring stringify

Is there any option for qs.stringify that will not encode the urls?
$ node
> var qs = require("querystring");
undefined
> qs.stringify({"url": "http://domain.com"});
'url=http%3A%2F%2Fdomain.com'
I want the folowing output:
'url=http://domain.com'
It's a little late, but for the next person, you can do:
querystring.unescape(myString)
Late answer again, but...
qs.stringify() has option encode:false which actually disable the URI encoding.
Qs.stringify documentation
You can also use it in nodejs request/request module as:
request({
url: 'http://url.domain'
qs: {
qs1: 'thisIsNotEncodedInTheRequest%20://асд'
},
qsStringifyOptions: {
// encoding: false /** (OLD VERSION - I think is deprecated yet) */
encode: false
}
});
Not directly, no. Although, if you are not escaping the value in the query string then there is hardly any benefit to using querystring at all. Mind as well just do: var q = 'url=http://domain.com'
EDIT: From looking at the source, the only way would be to change the behavior of (i.e. overwrite) the querystring escape() function - which is possible but not a good idea.
Here is the answer:
qs.stringify({url: "http://domain.com"}, { encodeURIComponent: uri => uri });
The option "encodeURIComponent: uri => uri" is to disable the encoding.
This worked for me
qs.stringify({url: "http://example.com"}, null, null, { encodeURIComponent: qs.unescape });

How to encode arbitrary string for request in Node.js?

I have a string like that: "abcde 李". It can be any string with non latin characters.
I want to encode it to use in request, so it will be "abcde %E6%9D%8E" and can be used for http.request.
I have tried this:
str.toString("utf-8");
or
var buffer = new Buffer(str);
str = buffer.toString('utf-8');
but none of them work. what is the proper way to handle this?
That string is already UTF-8. It looks like you're trying to escape it for use in an HTTP query string, so try this:
var qs = require('querystring');
qs.escape('abcde 李'); // => 'abcde%20%E6%9D%8E'

Resources