I have a variable e.g. var merchanttoken = requestConfig.merchant_connect_token, how do I pass it when making an call to an endpoint e.g
request.get('http://pi.call/v2/{merchanttoken}/info, function)
I need to pass the variable in the url.
I recommend pass it as a POST request instead of GET. Because auth token is sensitive content.
Still if you prefer GET change URL format to this.
http://pi.call/v2/info?merchanttoken={merchanttoken}
You can access it at server side nodejs by
var url = require('url');
var url_parts = url.parse(request.url, true);
var merchanttoken = url_parts.merchanttoken;
Related
I'm trying to make an esports web page, but first I'm playing with the API.
the problem is that when I want to simplify my url like this.
var url = "https://api.pandascore.co/lol/champions/2524?token="+accesskey+""
var accesskey = "example"
sends me an 401 error or accesskey invalid but when I code it like this
var url = "https://api.pandascore.co/lol/champions/2524?token="example"
it works, so I don't know if I'm simplifying bad my code :(.
+"" isn't necessary try with just:
var url = "https://api.pandascore.co/lol/champions/2524?token="+accesskey
You can also do it with template literals like this
var url =`https://api.pandascore.co/lol/champions/2524?token=${accesskey}`;
In the following node.js example:
var url = require('url');
var urlString='/status?name=ryan'
var parseObj= url.parse(urlString);
console.log(urlString);
var params = parseObj.searchParams;
console.log(JSON.stringify(params));
the property searchParams is undefined. I would expect searchParams to contain the parameters of the search query.
As you see in https://nodejs.org/dist/latest-v8.x/docs/api/url.html#url_class_urlsearchparams
searchParams is a proxy to an URL object. You must obtain a new URL complete object (with domain and protocol) and then you can use searchParams:
var url = require('url');
var urlString='https://this.com/status?name=ryan'
var parseObj= new url.URL(urlString);
console.log(urlString);
var params = parseObj.searchParams;
console.log(params);
Other way is using the query attribute (you must pass true as second parameter to url.parse):
var urlString='/status?name=ryan'
var parseObj= url.parse(urlString, true);
console.log(parseObj);
var params = parseObj.query;
console.log(params);
It is recommended to use: var parsedUrl = new URL(request.url, 'https://your-host'); instead of url.parse
url.parse shouldn't be used in new applications. It is deprecated and could cause some security issues: as stated here
For exemple this is my server with a simple API :
var express = require('express');
var rzServer = express();
rzServer.use(bodyParser.urlencoded({extended:true}));
rzServer.use(bodyParser.json());
app.get('/url', function(req, res) {
console.log(req.query.data); // String
console.log(JSON.parse(req.query.date)); // Object
});
req.query.data is interpreted as a string but it's a JSON Object.
Is it possible with the body-parser package to parse the querystring ?
Thanks.
body-parser is a middleware to parse body (it's its name). If you want to parse the query string, so you need another middleware for that.
Another thing : GET requests normally don't take any JSON parameters (no body). If you need to send a true JSON, perhaps you're not using the good HTTP method. Try to use a POST request, or create a true query string (http://expressjs.com/fr/api.html#req.query).
I want to send a GET request using request module. Here's the code:
var requestModule = require('request');
var url = require('url');
var myUrl = 'www.google.com';
var myUrlObj = url.parse(myUrl);
requestModule(myUrl, myUrlObj , callback);
but it doesn't work because myUrlObj has a null value for its "protocol" attribute.
The same code works when:
var myUrl = 'http://www.google.com'
Why is it so rigid?
Also I tried doing the following to get around this problem:
if ( myUrlObj.protocol == null ) {
myUrl = "http://" + myUrl;
myUrlObj = url.parse(myUrl);
}
But some websites use https, while others use http. So, the above code fails for websites that use https, and the require module throws an exception.
If the URL comes from user input, default to http:// and let them enter a protocol for HTTPS. Encourage them to enter a protocol. Most HTTPS websites will redirect you from the HTTP url to the HTTPS URL. You can make the request module follow redirects using the example here.
How can I allocate an instance of a URL object using node.js from an existing url string?
Something like this:
var url = require('url');
var myurl = new url("http://google.com/blah");
I can't seem to find any mention/example of this anywhere.
var url = require('url');
var myurl = url.parse('http://google.com/blah');
You can now use
myurl.hostname // google.com
myurl.pathname // /blah
and so on..
http://nodejs.org/api.html#url-302
You very rarely (if ever) need to use the new keyword in relation to the built-in modules, as long as you use the documented functions.