Well, this is an unusual question and I am not sure where should I put in which site of stackoverflow, so if anybody can tell me I will gladly ask in that site.
Question
So, I am developing an ExpressJS backend and was testing that in postman.
The following code is of my interest.
response.status(200);
result.message = "Please Fill Registration Details";
result.data = {
code: 010101
};
result.status = "Successful";
This is a simple code, no issue here, but when I checked in postman, the output is:
{
"message": "Please Fill Registration Details",
"data": {
"code": 4161
},
"status": "Successful"
}
How did the code 010101 change to 4161 ?
I first thought that postman was considering 010101 to be binary and converting it to decimal, but the decimal value is 21 and hexadecimal number is 15.
So, how is this happening ? Has anyone experienced this before ?
I still need to check this API in production and in actual devices.
I will update my findings.
It consider 010101 as an octal number, that is how JavaScript works. The reason it consider that as an octal number is because it is not surrounded by the quotes.
Here is an example of JavaScript:
console.log(010101); //4161
So, to fix your code, surround your value in quotes:
result.data = {
code: "010101"
};
Javascript understands numbers starting with 0 to be octal (base 8). 10101 in Octal has a decimal value of 4161. If you want to pass the code with a leading zero, pass it as a string "010101".
You selected JSON formatting in postman...
Besides that JS does not follow type rules,
let var1 = 010101;
let var2 = Number(010101);
Related
I'm trying unit testing for the first time with Jest on a personal project, and some of my tests failed even though the data received was the exact same as expected, here's an example:
test("decrypt method works correctly", () =>{
const myAES = new rijndael("", "0bdfa595235c307a105d593fb78fbb13", { key: "SOME 128 BIT KEY", bits: 128, rounds: 9 })
expect(myAES.decrypt()).toStrictEqual({
"c": "0bdfa595235c307a105d593fb78fbb13",
"p": "ATTACK AT DAWN!",
"errors": []
})
}
So then I tried to check if it's a problem with Jest or my code:
const r = myAES.decrypt()
console.log(typeof r.p) // string
console.log(r.p === "ATTACK AT DAWN!") // false
Which just made me even more confused as the strings look the same. The code that I'm testing is an AES encryption function (Don't worry it's just a personal project for fun won't be used in production) that processes text as nodeJS Buffers, and in the end uses the toString() method to convert it back to a string. I'm thinking that may be why I'm having issues, but can't seem to find exactly why. Would love it if someone could point me in the right direction so I can get rid of this error. Thank you in advance.
P.S. I'll save everyone the pain of reading my AES implementation for now as I don't think it's a problem with the encryption but let me know if that's necessary
Ok, so turns out it was a stupid mistake where I overlooked the series of null bytes that tend to end up in the end of the buffer after decryption. While toString() will turn the buffer into the string I want the computer will not recognise it as the same string. So all I had to do was strip the null bytes that follow. Assuming that the null bytes should only appear at the end of the string as they normally do:
const i = buffer.indexOf(0x00)
const result = buffer.slice(0, i).toString() //solves the problem
My PayPal IPNs are validating fine, except for those with a txn_type of recurring_payment. When I pass the message to the validation endpoint, I'm converting the body into a query string using
var verificationString = '?cmd=_notify-validate';
Object.keys(body).map((key) => {
verificationString = verificationString + '&' + key + '=' + body[key];
return key;
});
My best guess is that this is messing with the order of the properties. PayPal's documentation states:
Your listener HTTPS POSTs the complete, unaltered message back to PayPal; the message must contain the same fields (in the same order) as the original message and be encoded in the same way as the original message.
But I didn't think Object.keys(body).map would rearrange anything in Nodejs. Any suggestions?
Found the solution. Turns out that PayPal allows user fields to contain things like backslash, newline characters, etc. This specific IPN had an address field with a \r\n newline between the street and apartment number. Using my original code, this was somehow being encoded different.
Instead of assembling the query string like in my original question, I now assemble it like this, since it preserves all characters and encoding:
var verificationString = '?cmd=_notify-validate&' + request.rawBody.toString();
And that solved the problem!
When setting a large number as the value for a Range (cell), the number that is written to the spreadsheet is different than the original number. E.g.
If I set 42300000000, the number in excel becomes -649672960. This doesn't happen with smaller numbers
I tested it with the basic project sample from Visual Studio. Just replaced the original loadSampleData function with:
function loadSampleData() {
var values = [
[4230, 42300, 423000],
[4230000, 42300000, 423000000],
[4230000000, 42300000000, 423000000000]
];
// Run a batch operation against the Excel object model
Excel.run(function (ctx) {
// Create a proxy object for the active sheet
var sheet = ctx.workbook.worksheets.getActiveWorksheet();
// Queue a command to write the sample data to the worksheet
sheet.getRange("B3:D5").values = values;
// Run the queued-up commands, and return a promise to indicate task completion
return ctx.sync();
})
.catch(errorHandler);
}
When I run the add-in, I get this in Excel:
4230 42300 423000
4230000 42300000 423000000
-64967296 -649672960 2093204992
Is this some kind of overflow? Am I doing something wrong?
Thanks!
Thanks for reporting this bug.
The problem seems to be in the JSON parser that we are using to deserialize the incoming request. It incorrectly assumes that any integer number fits in int32_t. The correct behavior would be to parse such large values as double despite of the fact that they are integers.
Since this isn't Excel code, a fix may take a long time.
Unfortunately, appending .0 or E0 at the end of these literals, doesn't drive the parser to parse these literals as double. As Charles Williams pointed out, enclosing the literals in single or double quotes serves your purpose [for some unknown reason].
I haven't been able to find a more deterministic work around. Other suggestions will be welcome.
Zlatko
When I explore this bug I get the following results
9876543210 and 9876543210.0 give 1286608618 - fail
9876543210.9 gives 9876543210.9 - works
'9876543210' and "9876543210" - do not result in a string (I would consider that a bug but maybe this is just a JS type confusion nasty) but give
9876543210 as a number (so a possible hack bypass to the original bug)
"'9876543210" gives '9876543210 which excel recognises as a string -
correct
using the following code with 1704.8017.1000
async function setValue() {
try {
await Excel.run(async (context) => {
let sheet = context.workbook.worksheets.getActiveWorksheet();
let range = sheet.getRange("C3");
range.values = [[ 9876543210.0 ]];
range.format.autofitColumns();
await context.sync();
});
console.log("Done!");
}
catch (error) {
OfficeHelpers.Utilities.log(error);
}
}
I'm working with the Readline module in NodeJS and would like to parse the content of what the user wrote as code. Meaning if someone writes:
{
name: "David",
age: 34
}
I should be able to JSON.stringify(content) and get:
{
"name": "David",
"age": "34"
}
How can I convert a string in to actual code, so it can be interpreted as a JavaScript object, thus be converted in to JSON using JSON.stringify()?
It's not entirely clear what you're asking, but, would JSON.parse() help you here? You'll want to wrap it in a try catch in case the input is not valid JSON.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
The trick to make this work is to use the VM module in a undocumented way as seen below.
let vm = require('vm');
let script = new vm.Script('x = ' + full_content);
let sandbox = script.runInThisContext();
converted = JSON.stringify(sandbox);
Basically you have to create a variable that will hold your string (JavaScript), which will be then converted in to proper JavaScript code thanks to .runInThisContext().
In this case the x variable will "disappear" and you don't have to think about that. But if you were to follow the NodeJS documentation example, and use .runInNewContext() then the x variable wouldn't go away, a you would have your object (in my case at least) assigned to the x variable.
Hope this helps :)
I have correlated the Token value taken from the following response snippet:
result.sessionToken = '7AFF3BA8\x2DD913\x2D4211\x2D990E\x2D7DF3AB5687B7';
Using the web_reg_save_param function as:
web_reg_save_param(
"TOKEN",
"LB=result.sessionToken = '",
"RB=';",
"ORD=1",LAST);
But in a later request I need to send the correlated value in the below format:
7AFF3BA8-DD913-4211-990E-7DF3AB5687B7
The value \x2D is to be substituted by -.
I am right now using the below 'C' and LR code for this:
strcat(pstr1,lr_eval_string("{RToken}"));
strcat(aSeparator,"\\");
for(a=0,b=0;pstr1[a]!=NULL;a++,b++)
{
if(pstr1[a]==aSeparator[0])
{
strcat(pstr2,"-");
pstr2[b+1]=pstr1[a+4];
a=a+5;
b=b+2;
}
pstr2[b]=pstr1[a];
}
lr_save_string(lr_eval_string(pstr2), "sessionToken");
I wanted a generic and another approach for this problem. I don't want to use web_convert_param function, but if there is a hidden trick to convert the string as desired I would like to know.
Thanks,
Ritika
Try This...lr_save_string(lr_eval_string("{TOKEN}"),"convertedtkn");