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, '&'));
Related
In IDE it is possible to set value of (connector) parameter through JavaScript.
Note: The IDE is IBM Security Directory Integrator (SDI). It is an eclipse based IDE that provides to write Java & JavaScript to perform ETL related activities. Since the questions is related to Java & JavaScript I believe if someone from Java/JavaScripts can help me understand the difference.
For eg: To set url parameter
// Get AL config object.
var gALCfg = task.getConfigClone(); //task is an in-built object provided in SDI
// Get AL settings object from AL config.
var gALSettings = gALCfg.getSettings();
// Get Parameters from TCB
var WSDLurl = gALSettings.getStringParameter("erPOSWSDLlocation"); ///we get the wsdl URL in this step.
var myURL = WSDL;
URL field on a connector can now be set with 'var myURL'
What difference would it make if I create a URL object and then set value of the (connector) parameter ? For eg:
var gALCfg = task.getConfigClone(); //task is an in-built object provided in SDI
var gALSettings = gALCfg.getSettings();
var WSDLurl = gALSettings.getStringParameter("erPOSWSDLlocation"); //we get the wsdl URL in this step.
var myURL = new Packages.java.net.URL(WSDL);
I have written a POSTMAN call to a server that responds with a list of items in JSON like below:-
{
"count": 6909,
"setIds": [
"1/7842/0889#001",
"2/4259/0166#001",
"ENT0730/0009",
"9D/11181#002",
"1/9676/0001#001",
"2/4718/0001#004",
"2/1783/0044#001",
"1/4501/0001#001",
"1/2028/0002#002",
"2/3120/0079#001",
"2/1672/0024#001",
"2/3398/0064#001"
}
I want to make calls to another server using the value of the setID each time and iterate through all of these so that I end up calling the server thousands of times to verify the response from that server. The problem I have is that the second server expects the set id to be in a form where the forward slashes are converted to underscores and the hashes to dots, so
"1/7842/0889#001"
becomes
"1_7842_0889.001"
I have code that converts one to the other in POSTMAN
var jsonData = pm.response.json()
for (var i=0; i<jsonData.setIds.length; i++)
{
var new_j = jsonData.setIds[i].replace (/#/g, ".");
var new_i = new_j.replace (/\//g, "_");
}
})
This works fine line by line it creates the right thing in the console of POSTMAN but obviously what I really need to do is save the entire JSON in the right form to a file and then read from that file line by line using the corrected data. I don't seem to be able to save the data in a file in the right form using my code and I suspect I am missing something simple. Is there a way to write a file line by line from in side postman or in a script and manipulate the data as I'm creating it?
Alternatively I guess I could read from the JSON I have saved i.e. the full response and iterate through that manipulating the data as a pre-request script?
I have tried to do something like this using environmental variables - so in my first call I do:
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable('setIds', JSON.stringify(jsonData));
and then in my second call to the express server where I want to send my payload I run a pre-request script that I thought would work using the env variable but this fails as it doesn't seem to like the {...
SyntaxError: Unexpected token {
I think there are probably some neat ways of solving this either doing all of this outside of POSTMAN in javascript but I'm a little lost where to start. Any help appreciated
Would tell you are plaing with content, but not setting it back to JSON object ??
jsonData.setIds[i] = new_i;
can help or you can use 2x replace it in a string and convert back to make it easier (in case there are no / or # somewhere else).
var src = {
"count": 6909,
"setIds": [
"1/7842/0889#001",
"2/4259/0166#001",
"ENT0730/0009",
"9D/11181#002",
"1/9676/0001#001",
"2/4718/0001#004",
"2/1783/0044#001",
"1/4501/0001#001",
"1/2028/0002#002",
"2/3120/0079#001",
"2/1672/0024#001",
"2/3398/0064#001"
//...
]
}
var str = JSON.stringify(src, null, 4);
str = str.replace(/\//g,'_').replace(/#/g,'.');
console.log(str);
var res = JSON.parse(str);
console.log(res);
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!
I'd like to perform a redirect, but I also wish to send additional information along with it.
I've tried to change the value of window.location.href but that doesn't seem to pass along the extra information.
I also get how I can do
$.get(
new_url,
{data : "mydata"},
function(data) {
alert('page content: ' + data);
}
);
and that will display the html content of the new page, but that doesn't help with actually getting there.
How can I achieve this?
Edit: I feel as if I must be phrasing this terribly because I'm pretty sure this is an easy/common task. This shouldn't be something that would require cookies - it should basically be like a post request (I think).
You have a few different options for this:
URI Variables - You can append extra data to the URL by appending a question mark (?) followed by a set of key-value separated by an ampersand (=) with each variable being separated by an ampersand (&). For instance, http://www.google.com/search?q=javascript+url+variables&ie=UTF-8 gives you a link to a Google search for "javascript url variables" using UTF-8 encoding. Your PHP code or JavaScript would need to handle passing along and processing these variables. If using JavaScript a nice library for processing URLs is URI.js or using PHP you can use the parse_url and http_build_query functions. You can use this with window.location.href; for instance: window.location.href = "http://www.google.com/search?q=javascript+url+variables&ie=UTF-8" (replace the Google URL with the one you created or set in a variable).
Storage API - You can use the localStorage or sessionStorage properties to store and retrieve information using JavaScript (information is stored in the user's browser - supported by IE 8 and newer and all other major browsers). Note that this is JavaScript only unless you grab the data with JavaScript and pass it to your PHP server through URL variables, form, AJAX request, etc.
Cookie - You can store additional information inside a cookie - however this is more difficult since you have to setup your variables as a parsable string (possibly JSON) and remember to encode/decode the string when setting/getting the cookie. I don't recommend this method.
IndexedDB API - This is a more advanced client-side/browser storage mechanism and currently only supported in IE 10 and newer (and nearly all other browsers). There are also still changes being made to the standard which means newer versions of browsers could break current implementations or be buggy. If all you need is simple key-value storage (not an SQL-like database) then you should stick with one of the above options.
You can use the window open method to redirect your user,and remember to use "_self"
window.open('url','_self');
Preferably you'd store the data in localStorage and fall back to a cookie (I really like js-cookie).
Here are the two helper functions you need to store and retrieve data:
function setMultiPageData(itemName, data) {
var dataStr = JSON.stringify(data);
var hasLocalStorage = typeof localStorage !== 'undefined';
if (hasLocalStorage) {
localStorage.setItem(itemName, dataStr);
}
else {
Cookies.set(itemName, dataStr, { path: '/' }); // path set to root to make cookie available on any page
}
}
function getMultiPageData(itemName) {
var data = null;
var hasLocalStorage = typeof localStorage !== 'undefined';
if (hasLocalStorage) {
data = localStorage.getItem(itemName);
}
if (!hasLocalStorage || data === null) {
data = Cookies.get(itemName);
}
var parsedObject = null;
try {
parsedObject = JSON.parse(data);
}
catch (ex) {
console.log(ex); // remove in production
}
return parsedObject;
}
usage:
var data = { first: 'this is the first thing', second: 'this is the second thing' };
setMultiPageData('stackoverflow-test', data);
// go to a new page
var retrievedData = getMultiPageData('stackoverflow-test');
if (retrievedData === null) {
console.log('something went wrong')
}
else {
console.log(retrievedData); // { first: 'this is the first thing', second: 'this is the second thing' }
}
Maybe this is simple, maybe this is a bug on Parse - would like to know if anyone has had the same problem and a possible solution.
What I'm trying to do:
I'm sending a JSON request from an app called FormEntry to my Parse app
The body comes in like this: json={"someLabel" : "someValue"}
I would like to take the entire body and create a Parse.Cloud.httpRequest over to Zapier to perform some functions.
Now, the problem seems to be this:
On random occasions (i.e. I have no idea why), the body is sent (as shown by the logs) where there is a trailing comma at the end of the last pair in the JSON object. e.g. like this json={"lastLabel" : "lastValue",}
The number of elements in 'normal' and 'incorrect' objects seem to be the same, so it's simply just another comma added. And I have no idea why.
My setup:
Using app.use(parseExpressRawBody()); only and not the standard app.use(express.bodyParser()); which doesn't provide access to the raw body.
Because parseExpressRawBody converts the body to a buffer I need to turn it back into a string to send it in the HTTP request in a meaningful way. Therefore I use: var body = req.body.toString();
When logging this var to the Parse console it looks to be format back from the buffer fine.
And that's about it. Nothing complex going on here but a real annoying bug that I just haven't found a sensible way of understanding. Would SUPER appreciate anyone who has seen this before or who could point me in a direction to focus on.
Just an update on this. Not a solution that answers why there is malformed JSON but a hack to get the right result.
The purpose of the HTTP request was to point over to Zapier so I wrote a Zapier script that would deal with the malformed JSON. Added here for anyone else who needs it.
"use strict";
var Zap = { newSubmission_catch_hook: function(bundle) {
var body = bundle.request.content;
var cleanTop = body.substring(5,body.length);
var cleanChar = cleanTop.length;
var condition = cleanTop.substring(cleanChar-2,cleanChar);
function testCase(condition,cleanTop) {
if (condition != ",}"){
console.log("Everything is fine, returning JSON");
return cleanTop;
}
else {
console.log("Nope! We have an error, cleaning end");
var cleanEnd = cleanTop.substr(0,cleanChar-2) + '}';
console.log("The object now ends with: " + cleanEnd.substr(-10));
return cleanEnd;
}
}
var newBody = JSON.parse(testCase(condition,cleanTop));
return newBody;
}
};