I fail to parse the first query string parameter using the qs npm package. What am I doing wrong?
I execute these commands in my console
import * as qs from './qs'
var addr = "https://www.somesite.se/?title=querystring&action=edit"
var parsed = qs.parse(addr)
After executing these commands parsed has the value:
{ 'https://www.somesite.se/?title': 'querystring',
action: 'edit' }
This is strange. Why is title not a property on the returned object? I would expect the value of parsed.title to be 'querystring'. but it is not. The value of parsed.title is undefined.
Why?
qs parses query strings. It does not parse URLs. Use a URL parser (new URL(addr).search.substring(1)) to get the query string from the URL first.
qs.parse("title=querystring&action=edit") should give you a correct answer.
Now that I think about it... why even use qs? new URL(addr).searchParams should already give you the params parsed...
The answer is: the qs library is using for parsing the query string only.
According to Wikipedia:
a query string is the part of a uniform resource locator (URL) which assigns values to specified parameters.
For example:
In your case, the correct codes should be:
var addr = 'title=querystring&action=edit';
var parsed = qs.parse(addr);
console.log(parsed); // {title: "querystring", action: "edit"}
To bypass the leading question mark, use ignoreQueryPrefix:
var addr2 = '?title=querystring&action=edit';
var parsed2 = qs.parse(addr2, { ignoreQueryPrefix: true });
console.log(parsed2); // {title: "querystring", action: "edit"}
Hopefully, that helps!
Related
For example, when a video is uploaded to a video sharing platform, it returns a callback url - A URL to notify that a video is completely uploaded.
The docs of the Video Indexer API, the callback URL made by the service will include the videoID in the query which I want to parse and use it in my own query string.
For example, if the callback url is 'https://test.com/notifyme?projectName=MyProject', the notification will be sent to 'https://test.com/notifyme?projectName=MyProject&id=12345abcde&state=Processed'.
I want to parse that id query string parameter, so that I can use it in my web page link:
What I tried to do was use substring and indexOf.
E.g: var queryString = url.substring( url.indexOf('?') + 1 );
But I am not sure if this is right approach, as I am not getting what I was hoping for.
you can just use :
var qs = url.split('=')[2].split('&')[0];
it will give you the id in this url
you can use split method to split a string into an array.
you can use URLSearchParams
https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams
var paramsString = "<url here>";
var searchParams = new URLSearchParams(paramsString);
var value = searchParams.get(key_here)
I am creating a temporary URI in vs code. It is needed for command vscode.diff.
I am following their example from here
The URI is parsed via the following command
let uri = vscode.Uri.parse('cowsay:' + what);
and read via the following command (from their examples)
const myProvider = class implements vscode.TextDocumentContentProvider {
provideTextDocumentContent(uri: vscode.Uri): string {
return cowsay.say({ text: uri.path });
}
};
It is stored in uri.path. The problem I am facing is that the data I want to store contains # in them. uri.path ignores all text as soon as the first # is encountered.
Is there a way to store data in a custom URI containing #.
e.g.
If my code is below
let textToStore: string = "print '1'# some comment";
// Storing in URI
let uri = vscode.Uri.parse('cowsay:' + textToStore);
The URI.path would only store print '1' in it while it should store print '1'# some comment. The character after # are ignored.
Is there a way to store # in a custom URI scheme in Vs code.
You might be looking for encodeURIComponent(). This function encodes certain characters that cannot be used in URL components, such as '#'. More information can be found here.
let textToStore: string = encodeURIComponent("print '1'# some comment");
I need a query-string to be in the format:
brand=samsung&model={"type":"touch"}
I am using the in built query-string module in Node.js
How would I form my object in order to output the query-string above?
My current object looks like this:
{ brand: samsung, model: { type: 'touch' } }
Which returns a query-string in the format:
brand=samsung&model=
I need to form the query-string without altering the URL.
This should do the trick:
const qs = require('querystring');
let object = {};
object.brand = "samsung"
object.model = JSON.stringify({
"type": "touch"
});
console.log(qs.stringify(object));
Here is a demo.
This is done with percent-encoding, the request will be interpreted as an object you described in your question.
We're using MobileFirst Foundation 8.0.0.00-20160822-2140 deployed to WebSphere Application Server V8.5.5.5.
How to pass json array object from on javascript adapter to another javascript adapter?
How to pass json array object with Apostrophe from on javascript adapter to another javascript adapter?
Is there a limit in the JSON size?
We have tried:
To call the second javascript adapter from the first javascript adapter:
var samplejsonarrayobject = [{a:"xxx",b:"yyy"},{c:"zzz",d:"aaa"}];
var res = MFP.Server.invokeProcedure({
adapter: "secondAdapter",
procedure: "secondAdapterFunction",
parameters: [samplejsonarrayobject]
});
To call the second javascript adapter from the first javascript adapter with stringify:
var samplejsonarrayobject = [{a:"xxx",b:"yyy"},{c:"zzz",d:"aaa"}];
var stringifysamplejsonarrayobject = JSON.stringy(samplejsonarrayobject);
var res = MFP.Server.invokeProcedure({
adapter: "secondAdapter",
procedure: "secondAdapterFunction",
parameters: [stringifysamplejsonarrayobject]
});
To call the second javascript adapter from the first javascript adapter with a very large JSON file:
var largesamplejsonarrayobject = [{a:"xxx",b:"yyy"}...................................{c:"zzz",d:"aaa"}];
var largestringifysamplejsonarrayobject = JSON.stringy(largesamplejsonarrayobject);
var res = MFP.Server.invokeProcedure({
adapter: "secondAdapter",
procedure: "secondAdapterFunction",
parameters: [largestringifysamplejsonarrayobject]
});
To call the second javascript adapter from the first javascript adapter with a very large JSON with Apostrophe symbol :
var apostrophesamplejsonarrayobject = [{a:"xx'x",b:"yyy"},{c:"zzz",d:"aaa"}];
var apostrophestringifysamplejsonarrayobject = JSON.stringy(apostrophesamplejsonarrayobject);
var res = MFP.Server.invokeProcedure({
adapter: "secondAdapter",
procedure: "secondAdapterFunction",
parameters: [apostrophestringifysamplejsonarrayobject]
});
Returned errors
For 1) test case:
[1/10/17 22:06:04:703 MYT] 00000131 MFP.Logger
java.lang.IllegalArgumentException: invalid type of element
For 2) test case: Able to pass the JSON.
For 3) test case: Unable to pass the JSON.
Caused by: com.fasterxml.jackson.core.JsonParseException: Unexpected
character ('s' (code 115)): was expecting comma to seperate ARRAY
entries at [Source : ['[{a:"xx'x",b:"yyy"},{c:"zzz",d:"aaa"}]; line 1,
column]
For 4) test case:
Caused by: com.fasterxml.jackson.core.JsonParseException: Unexpected
character ('s' (code 115)): was expecting comma to seperate ARRAY
entries at [Source : ['[{a:"xx'x",b:"yyy"},{c:"zzz",d:"aaa"}]; line 1,
column]
Solve it by:
jsonString = replaceAllString(jsonString, "'s", "'s");
function replaceAllString(stringJ,search, replacement) {
return stringJ.replace(new RegExp(search, 'g'), replacement);
}
This is indeed a defect and a regression from MobileFirst Platform Foundation 7.1. The development team will be working on fixing this.
To track this issue the following APAR has been opened: PI74759: CAN'T PASS A JSON OBJECT OR A JSON ARRAY AS A PARAMETER TO MFP.SERVER.INVOKEPROCEDURE
Update: Install the latest 8.0 iFix to obtain a fix for this issue, which resides on the server, so a new server installation is required.
I am developing restAPI using Node.js. In that the following is the URL for my API:
http://xxxxxxxx:3000/map70/places?node[level0=12&level1=34&level2=234][ids=1,2,3,4][key=xxxxxxxxxx][val=2011]
In this i have to get the each parameters level0,level1,level2,ids,key,value.
In normal url format like the below:
http://xxxxxxxx:3000/map70/places?key=xxxxxxx&val=2011&level0=2&level1=33&level2=602
I can parse like req.query.level0.. .But for the above url how can i parse it.I searched a lot,but i cant find the right solution.Help me to solve this..Thanks in advance....
Is there a name for this style of query parameter? I have never heard of it. If there's some standard around it, you might be able to find some obscure module that'll parse it. Otherwise, it's very likely you'll have to parse the string yourself.
You can get hold of the thing with Node's url module:
var url = require('url');
var parsed = url.parse('http://xxxxxxxx:3000/map70/places?node[level0=12&level1=34&level2=234][ids=1,2,3,4][key=xxxxxxxxxx][val=2011]');
var query = parsed.query;
At this point, query now contains node[level0=12&level1=34&level2=234][ids=1,2,3,4][key=xxxxxxxxxx][val=2011], and you can start to pull it apart with string operations.
[Update]
I'm only going off the URL you posted, so you'd probably need to write some tests cases to ensure all the parameters you care about get parsed, but I was able to get something like this:
var querystring = require('querystring');
var q2 = query.slice(5, -1);
// q2 == "level0=12&level1=34&level2=234][ids=1,2,3,4][key=xxxxxxxxxx][val=2011"
var q3 = q2.replace(/\]\[/g, '&');
// q3 == "level0=12&level1=34&level2=234&ids=1,2,3,4&key=xxxxxxxxxx&val=2011"
querystring.parse(q3);
// returns:
// { level0: '12',
// level1: '34',
// level2: '234',
// ids: '1,2,3,4',
// key: 'xxxxxxxxxx',
// val: '2011' }
You can combine it all together into:
querystring.parse(query.slice(5, -1).replace(/\]\[/g, '&'));