Combine two urls into one in node.js - node.js

I am trying to combine two urls into a single url.
var access_token = 138def4a4e;
var url = "https://graph.facebook.com/app/?access_token=";
I want the final url to be:
url = "https://graph.facebook.com/app/?access_token=[access_token]";
How to do that in node.js? I tried using url. Resolve but it was of no use.
pls help
TIA

As noted above the selected solution is unsafe.
The following snippet should be preferred:
const accessToken = '138def4a4e';
const someValidUrl = 'https://graph.facebook.com/app/?foo=bar'; // note the querystring param
const url = new URL(someValidUrl);
url.searchParams.append('access_token', accessToken);
console.log(url.href);
As you can notice it's able to manage an Url that contains query parameters and the URLencoding of the querystring params.

I am assuming your code looks like this:
var access_token = '138def4a4e'
var url = 'https://graph.facebook.com/app/?access_token='
If so, the answer is:
var combined = url + access_token
For a final answer of:
var access_token = '138def4a4e'
var url = 'https://graph.facebook.com/app/?access_token='
url += access_token
console.log(url)

Related

How to Work With Uri.encodeComponent in Dart/Flutter?

I am working with a customized webview, there I need to pass a url as String.
In this url, I need to pass some query parameter, for various reason, I can't pass'm as body rather url;
Problem is, one of my parameter contain special character such as $ , \ , &. To use the parameter as string I used Uri.encodeComponent to encode the param, but I am facing a strange problem.
if I make the url using Uri.encodeComponent , the url is not working
url = "soemthing.php?acc_token=" + Uri.encodeComponent(str);
But, If I print the output, and make the url manually, the url work just find.
Full Scenario ->
var str = await GetUserLocalStorage().accessToken();
//str = "\$3d\#"
var token = Uri.encodeComponent(str);
//token = %243d%23
//this maybe not exactly same, using it for simulation
print(token);
url = "soemthing.php?acc_token=" + token; //this is not working
//tried token.toString() too!
url = "soemthing.php?acc_token=" + "%243d%23" // this is working
url = "soemthing.php?acc_token=" + Uri.encodeComponent(str);
//This is not working

Concatenate strings in flutter adds ""

This is rather a silly question but I can't seem to figure it out. I'm building a flutter app that communicates to an API and in order for the user to be authenticated it sends an token.
Whenever the user logs in I save the token in the Shared Preferences and then when I make an request add the token to the headers or in this case I need it in the url.
However, whenever I concatenate the strings: the server url and the token it adds extra "" to the token. something like:
http://10.0.2.2:64342/chatHub?access_token="token-value"
instead of
http://10.0.2.2:64342/chatHub?access_token=token-value
here's the code:
var preferences = await SharedPreferences.getInstance();
token = preferences.getString(token_key);
var url = '$serverURl?access_token=$token';
As far as I understand your question, I would like to answer it.
That's not possible!
var serverURl = 'http://10.0.2.2:64342/chatHub';
var token = 'token-value';
var url = '$serverURl?access_token=$token';
print(url);
It just prints the correct one!
You can check the string that is stored in the SharedPreferences! That maybe with quotes.
Okay, I figured it out. Since I was sending only the token from the API. I was receiving it with the "" in it.
Instead I now send a json with the token, like: { "token": "token_value"} and then decode it to get the actual value. So when I store it the shared preferences it doesn't keep the "".
So in the backend:
return Ok(new {token = generatedToken});
and in dart
var tokenJson = json.decode(response.body);
var token = tokenJson['token'];
preferences.setString(token_key, token);
Thanks to everyone that helped :)

How to configure client for access with authsecret?

I'm using the client and I need to call a service using authsecret parameter.
If I ad this param to the base url it give me a serialization error.
String baseUrl = AppConfig.GetAppApiUrl();
var client = new JsonServiceClient(baseUrl.AddQueryParam("authsecret","secretz123!"));
var c = client.Send(new ComuneRequest { Id = "A001" });
Using Fiddler I discovered that the request that the client generate is incorrect:
POST
http://192.168.0.63:820/?authsecret=secretz123%21/json/reply/ComuneRequest
So, what I have to do to make the client create a request in a correct format?
It needs to be sent as a Request Parameter (i.e. QueryString or FormData) which you can do using HTTP Utils with:
var url = baseUrl.CombineWith(requestDto.ToUrl()).AddQueryParam("authsecret", secret);
var res = url.GetJsonFromUrl().FromJson<MyResponse>();
Otherwise since AuthSecret is not a property on your Request DTO you wont be able to send it as a Request Parameter in the Request Body, but you should be able to send the param in the Request Headers with:
var client = new JsonServiceClient(baseUrl) {
RequestFilter = req => req.Headers[HttpHeaders.XParamOverridePrefix+"authsecret"] = secret
};

ServiceStack Raw Client query string

This should be simple, but I must be using the wrong key words to find the answer.
How can I output the raw query string that the jsonserviceclient is generating when sending a request to the server? I know I could use fiddler or something else to snoop the answer to this, but I'm interested if there is something like:
var client = new JsonServiceClient("http://myService:port/");
var request = new MyOperation
{
SomeDate = DateTime.Today
};
Console.Out.Writeline(client.AsQueryString(request));
You can use the Reverse Routing extension methods to see what urls different populated Request DTOs would generate, e.g:
var relativeUrl = new MyOperation { SomeDate = DateTime.Today }.ToGetUrl();
var absoluteUrl = new MyOperation { SomeDate = DateTime.Today }.ToAbsoluteUri();

How to extract complete URL without HTTP in Node.js?

I want to extract the complete URL except HTTP. I have used Domain and url.
Here is my code
var url = require('url');
var domain=require('domain.js');
var url_parts = 'http://static01.nyt.com/images/2014/11/17/business/billboardjump/billboardjump-master675.jpg';
var website=domain(url.parse(url_parts));
var querystring = (url.parse(url_parts, true)).path;
console.log(website+querystring);
But only I am getting
'nyt.com/images/2014/11/17/business/billboardjump/billboardjump-master675.jpg'
instead of
'static01.nyt.com/images/2014/11/17/business/billboardjump/billboardjump-master675.jpg'
Thanks in advance.
just calculate protocol length and drop it:
var u = 'http://static01.nyt.com/images/2014/11/17/business/billboardjump/billboardjump-master675.jpg';
var protocol = url.parse(u).protocol;
console.log(u.slice((protocol + '//').length));
const urlString = "https://stackoverflow.com/questions/26987567/how-to-extract-complete-url-without-http-in-node-js"
const [protocol, urlWithoutProtocol] = urlString.split('://');
console.log(protocol, urlWithoutProtocol)
if (!Str.match(/^(http|https)/i))
Str = "https://" + str;
if the Str doesn't have a ^ at beginning, add it.

Resources