I'm building a server in Node.js that receives data from Javascript on a website. The data is being sent using jQuery's getJSON method, like so:
$.getJSON(
url,
{
id: track.id,
artist: track.artist,
title: track.title,
album: track.album,
filename: track.filename,
user: track.user,
userId: track.userId,
room: track.room,
timestamp: track.timestamp
}
);
I'm then trying to get at the data using Node's url module like this:
var urlObj = url.parse(request.url, true);
var jsonCallback = urlObj.query["callback"];
This works fine most of the time, but it fails when one of the parameters contains an apostrophe. It looks like it's stopping parsing the query string at the apostrophe. Here's what console.log prints for two different query objects:
{ callback: 'jQuery15105242477038409561_1304925579219',
id: '6c91c74db064c93f1f020000',
artist: 'Radiohead',
title: 'Everything In Its Right Place',
album: 'Kid A',
filename: '01 Everything In Its Right Place.m4a',
user: 'Cowrelish',
userId: '82a89b4df7a9120305000000',
room: 'qwantz',
timestamp: '1304924972555',
_: '1304925611362' }
{ callback: 'jQuery15105242477038409561_1304925579221',
id: '798cc74dfcce4337f7010000',
artist: 'Toy Dolls',
title: 'The Final Countdown',
album: 'HOLY SHIT!!! IT' }
The album for the second one is "HOLY SHIT!! IT'S THE FINAL COUNTDOWN!", as you can see it's been truncated at the apostrophe.
I've tried escaping the apostrophe in the client code, but all I get then is "album: 'HOLY SHIT!!! IT\\'". It's escaping the slash, but not the apostrophe.
Is this a bug or a feature? Should I be escaping apostrophes in a different way on the client, or looking to fix a bug in Node.js?
You shouldn't have to escape on the jquery side, and it doesn't appear to be a node bug (at least with latest release).
I tried to duplicate your problem and wasn't able to using node 0.4.7.
I did this:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$.getJSON('/', {test:"it's a test"});
});
</script>
with node code that just did this on the incoming request:
console.log(require('url').parse(req.url, true));
which gave me:
{ test: 'it\'s a test' }
So, doesn't seem to be a general problem with either jQuery or node.
Have you looked at the full url before it is parsed? You should be able to just copy and paste that into the node interactive shell and do something like this to test:
require('url').parse('http://www.example.org/?q=it\'s working', true);
Anything between the browser and the web server that might be touching the url?
Related
I am trying to understand eth_signTypedMessage with Metamask and a little toy example.
Code in Browser (using Metamask and Chrome):
ethereum.request({ method: 'eth_signTypedData', params: [
[
{
type: 'string',
name: 'testname',
value: '1234567890'
}
],
publicAddress
] }).then((signedData) => {
SendSignedDataToBackend(signedData);
});
I am trying to figure out how to calculate the message hash of the typed data, but I can't really get it straight with the standard:
Backend php code (using the old php ECRecover):
$typeHash = Keccak::hash('(string testname)',256); // Also no success without the brackets
$encodeData = keccak::hash("1234567890",256);
$typedMsgHash = keccak::hash(hex2bin($typeHash.$encodeData),256);
$ecRecover = new EcRecover();
$address = $ecRecover->personalEcRecover($typedMsgHash,$signedData);
This results in the wrong public address. Any advise?
After going through the code of Metamask, it seems that Metamask signs the plain message:
sign(message)
and not as I expected
sign(keccak256("\x19Ethereum Signed Message:\n" + len(message) + message)))
This does not seem to follow EIP-712, but maybe I misunderstood it. Removing the extra hash calculation of "\x19Ethereum ..." on the server solved the problem. $typeHash calculation is without the brackets in the code of my question. Also need to add an extra "0x" in my code:
$typedMsgHash = '0x'.keccak::hash(hex2bin($typeHash.$encodeData),256);
I'm building a chatbot that retrieves a youtube link based on what the user has asked.
I'm using Dialogflow for the bot and using nodejs for the coding.
Right now, everything works perfectly but my problem is retrieving the link from the API response.
using console.dir(results), shows the following:
This is how the response looks like:
[ { id: 'gWNUg_v25dw',
link: 'https://www.youtube.com/watch?v=gWNUg_v25dw',
kind: 'youtube#video',
publishedAt: '2017-08-24T14:00:11.000Z',
channelId: 'UCDZ56yQ05d_ikcwcTG9bAiA',
channelTitle: 'Code to Create',
title: 'How to make a Chatbot with Dialogflow - API.ai',
description: 'In this video, Google Developer Expert, Faisal Abid will show us how to create a chatbot with Google\'s latest API.ai API.ai can be integrated with multiple chat ...',
thumbnails: { default: [Object], medium: [Object], high: [Object] } } ]
I have tried using results.link to retrieve the link but Im getting undefined, so my question is how to retrieve the link from the response block?
search(txt1, opts, function(err, results) {
//var data1 = JSON.parse(results);
//srchRes = data1.link;
if(err) return console.log(err);
console.dir(results);
});
uncommenting the 2 lines above and console.dir(srchRes) returns this error:
SyntaxError: Unexpected token u in JSON at position 0
We don't have your API call code logic so from here, we could only assume results is the API's response and given your exemple about how the response look like, then we see results is an array of object.
JSON.parse actually deserialize a string which represent an object given the JSON format.
JSON.stringify is the contrary, it serialize your object to a string with the JSON format.
Your goal is to reach the link properties of a particular object within the array, which is why JSON.parse(results) makes no sense.
You should be able to reach linkproperties for each object by simply iterate over the array of object results
for (const result of results) {
console.log(`the link : ${result.link}`)
}
I would like to break down the URL of the website with regex. The URL is similar as follows:
https://product.testing.com/intro/index.aspx?source=newsletter&product=watch&brand=rolex
The regex that I used is as follows:
(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?(product\.)(testing\.)(com\/)(.*)(\?|\?)([^=]\w+(?=&))
The first question is, I would like to chop the part after aspx? into pieces i.e source=newsletter, product=watch etc, and the code does not work for the last part, What did I do wrong and how should I change?
The second question is, The domain name part is a kind of hard coding...how can I make it better and more flexible e.g can be applied to https://contact.testing.com/contactoursales/index.aspx?
Thank you for your help in advance!
I recommend using the url package, instead of a regex to parse an URL.
const URL = require('url');
const url = 'https://product.testing.com/intro/index.aspx?source=newsletter&product=watch&brand=rolex';
// Pass true to parse the querystring too
const parsed = URL.parse(url, true);
Which will output:
Url {
protocol: 'https:',
slashes: true,
auth: null,
host: 'product.testing.com',
port: null,
hostname: 'product.testing.com',
hash: null,
search: '?source=newsletter&product=watch&brand=rolex',
query: { source: 'newsletter', product: 'watch', brand: 'rolex' },
pathname: '/intro/index.aspx',
path: '/intro/index.aspx?source=newsletter&product=watch&brand=rolex',
href: 'https://product.testing.com/intro/index.aspx?source=newsletter&product=watch&brand=rolex' }
I would like to chop the part after aspx? into pieces i.e
source=newsletter, product=watch etc, and the code does not work for
the last part, What did I do wrong and how should I change?
Passing true as second parameter to url.parse will parse the querystring for you.
console.log(params.query);
/* {
source: 'newsletter',
product: 'watch',
brand: 'rolex'
} */
If you're not using node.js you can use webpack to use url package on the browser.
webpack url-parser.js -o url-parser.min.js
My current Problem is: i need to be able to perform the following call:
https://{Net-IP}:{Net-Port}/rest/devices/{deviceName}.{deviceID}
First of all defining a host variable such as:
host: {Net-IP}:{Net-Port} is not possible.
Second, if i then try to implement the Path-Parameter in the paths such as:
/devices/{deviceName}.{deviceID}/measurePoints:
get:
summary: Method returns list of measure points
parameters:
- name: deviceName
in: path
type: string
required: true
description: device Name
- name: deviceID
in: path
type: string
required: true
description: device ID
then it says Path templating is not allowed.
I need this type of calls to be translated for Postman, i appreciate your help!
Why don't you make use of environmental variables/constants etc.
I am trying to use the find method to return documents that meet multiple criteria like so:
db.issues.find({ 'title.name': name, 'title.year': year, 'title.publisher': publisher }, function(err, issues) { ...
I get an empty array back as a result.
If I try the same query directly in the mongodb console, I get the results back as expected.
In mongojs, I am able to retrieve results if I only supply one attribute, like so:
db.issues.find({ 'title.name': name }, function(err, issues) { ...
So, how do I perform a search using multiple attributes? Do I need to use a different method?
UPDATED 2/2/2014 7:56
The issue schema is as follows:
{
title: { name: 'Some title name', year: 1999, publisher: 'Some publisher' },
number: "122",
variant: null,
print: "1",
year: "1999",
certification: { type: "Some certification", grade: 10 },
url: "http://www.someurl.com",
sales: []
}
There is no difference in how mongojs handles queries as it's intention is to mimic ( somewhat ) the behavior of the mongo shell.
Are you sure your criteria actually returns something? As it stands you possibly have an issue there. If you still cannot diagnose this, post up the result you expect to get and some of your schema as an edit. Don't forget to check your var's that numbers aren't strings and vice versa.