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 });
Related
I am doing Google Oath2 Implementation. For a particular authorization_code, I was constantly getting invalid_grant. I checked the value and found that the query string value was encoded.
Here is an example:
const parser = require('url');
url ='http://example.com/test?param=4%2F12'
const q = parser.parse(url, true).query
console.log(q)
My output here is
{ param: '4/12' }
I want my output to be
{ param: '4%2F12' }
as the correct auth code is a string with value 4%2F12. How do I implement this?. There may be
many manual ways to do this. Anything that needs a minimalistic code effort would be appreciated. Thanks in advance!
Simple. Just encode again the param using encodeURIComponent.
Example:
console.log(encodeURIComponent("4/12")) // Output: 4%2F12
/*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.
I have a nodejs express web server running on my box. I want to send a get request along with query parameters. Is there any way to find type of each query parameter like int,bool,string. The query parameters key value is not know to me. I interpret as a json object of key value pairs at server side.
You can't, as HTTP has no notion of types: everything is a string, including querystring parameters.
What you'll need to do is to use the req.query object and manually transform the strings into integers using parseInt():
req.query.someProperty = parseInt(req.query.someProperty);
You can also try
var someProperty = (+req.query.someProperty);
This worked for me!
As mentioned by Paul Mougel, http query and path variables are strings. However, these can be intercepted and modified before being handled. I do it like this:
var convertMembershipTypeToInt = function (req, res, next) {
req.params.membershipType = parseInt(req.params.membershipType);
next();
};
before:
router.get('/api/:membershipType(\\d+)/', api.membershipType);
after:
router.get('/api/:membershipType(\\d+)/', convertMembershipTypeToInt, api.membershipType);
In this case, req.params.membershipType is converted from a string to an integer. Note the regex to ensure that only integers are passed to the converter.
This have been answered a long ago but there's a workaround for parsing query params as strings to their proper types using 3rd party library express-query-parser
// without this parser
req.query = {a: 'null', b: 'true', c: {d: 'false', e: '3.14'}}
// with this parser
req.query = {a: null, b: true, c: {d: false, e: 3.14}}
let originalType = JSON.parse(req.query.someproperty);
In HTTP, querystring parameters are treated as string whether you have originally sent say [0,1] or 5 or "hello".
So we have to parse it using json parsing.
//You can use like that
let { page_number } : any = req.query;
page_number = +page_number;
Maybe this will be of any help to those who read this, but I like to use arrow functions to keep my code clean. Since all I do is change one variable it should only take one line of code:
module.exports = function(repo){
router.route('/:id',
(req, res, next) => { req.params.id = parseInt(req.params.id); next(); })
.get(repo.getById)
.delete(repo.deleteById)
.put(repo.updateById);
}
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.
I have the following GreaseMonkey Script:
GM_xmlhttpRequest({
method: 'GET',
url: "http://www.testurl.com",
headers: {
'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey/0.3',
},
onload: function(responseDetails) {
var tagString = responseDetails.responseText;
var range = document.createRange();
range.selectNode(document.body);
var documentFragment = range.createContextualFragment(tagString);
How do I now extract stuff from documentFragment? documentFragment.getElementById(''), document.body etc all returns undefined.
I suspect this is due to the createContextualFragment method returning a XPCNativeWrapper object, but how do I work around this to access the underlying DOM?
Thanks
Not sure if this is answered by Load and parse remote url with greasemonkey
Depending what you want to do, might be easier to include jquery..
I suspect this is due to the
createContextualFragment method
returning a XPCNativeWrapper object,
but how do I work around this to
access the underlying DOM?
This is done using wrappedJSObject:
var documentFragment = range.createContextualFragment(tagString).wrappedJSObject;