Unexpected decodeURI output - javascript

I send post request to my express server with body payload
handle: "new-text-collection-tech-gen"
raw: "Нова коллекція"
title: "%D0%9D%D0%BE%D0%B2%D0%B0%20%D0%BA%D0%BE%D0%BB%D0%BB%D0%B5%D0%BA%D1%86%D1%96%D1%8F"
But i receive unexpected encoding of Cyrillic characters in body
body: {
title: '%D0%9D%D0%BE%D0%B2%D0%B0%20%D0%BA%D0%BE%D0%BB%D0%BB%D0%B5%D0%BA%D1%86%D1%96%D1%8F',
raw: 'PP>P2P0 P:P>P;P;P5P:QQQ',
handle: 'new-text-collection-tech-gen'
}
And If I run decodeURI(title, 'utf-8') I expected to get back "Нова коллекція" but I get "PP>P2P0 P:P>P;P;P5P:QQQ"
I tried to use decodeURIComponent, tried not to set utf8 explicitly, tried to add app.use(express.json({ encoding: 'utf-8' }));
Please, help me to debug, where to look.
I use "express": "^4.18.2"
I am not sure what part of the code is relevant to provide, so just ask when having a question

Related

parse data to CSV send email use nodeMailer Node.JS

I've been looking nodeMailer documentation about attachment, the issue I have I need send a string of data like below, I need to convert them to CSV then send it through nodeMailer.
Saw another post of an array to CSV, mention I need to parse before I send it, so I tried to use papaparse the data and pass in their custom attachment, but I will get an error.
Set up in Node
const attachment =
""test1,","test2,","test3,","test4,""
const parseInfo = papaparse.parse(attachment, {delimiter: ",", newline: ""})
const options = {
to: toAddress,
from: fromAddress,
subject: subject,
text: text,
html: html,
attachments:{ // define custom content type for the attachment
filename: 'sample.csv',
content: parseData,
contentType: 'text/csv'
},
};
Error message
An error occured while sending the email ---:TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be one of type string or Buffer. Received type object
Do I miss anything? Thanks for the help!
My data is already a string, so I don't need parse date, I directly pass in as content, then it' works. Got to make sure file name with .csv and contentType: 'text/csv'.
I hope that helps someone have the same issue as me.
const attachment =
`"test1,","test2,","test3,","test4"`
const options = {
to: toAddress,
from: fromAddress,
subject: subject,
text: text,
html: html,
attachments:{ // define custom content type for the attachment
filename: 'sample.csv',
content: parseData,
contentType: 'text/csv'
},
};

Angular cannot get file download from express using res.download

In my application I create a file on the backend, and I wish then to deliver that to the user via a browser download. I have tried numerous
Here is the express backend:
app.get('/download', (req, res) => {
res.download('filename.txt', function (err) {
console.log(err);
});
})
The console call isn't returning, so presumably no error. Here is what I am doing in the front-end, following advice here and here:
window.open('localhost:3000/download');
The result is that I get a blank window popping up, but no download. I have also tried this:
const filePath = 'localhost:3000/download/';
const link = document.createElement('a');
link.href = filePath;
link.download = filePath.substr(filePath.lastIndexOf('/') + 1);
link.click();
But in this case, nothing happens.
What am i doing wrong? I at a loss even how to debug any further.
Update 1
Thanks #jal_a, I've made progress. I realise now that if I manually create a window and enter the url (as suggested) the download works. However, when the window is launched from the application using window.open(url) where url is the same, the window opens but the download doesn't initiate. If I then go to the created window, click in the url and press return ... voila! It works! How can I make the download initiate from the application launched window?
Update 2
Thanks #IceMetalPunk, I tried that and I'm getting the following error in the console - the file I'm trying to download is gps data in gpx format - which I can see in the response, but it seems to be expecting JSON?? Do I need to do any pre-processing to send a file?:
HttpErrorResponse {headers: HttpHeaders, status: 200, statusText: "OK",
url: "http://localhost:3000/download/", ok: false, …}
error:
error: SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse
...
text: "<?xml version="1.0" encoding="UTF-8"?>
↵<gpx version="1.1" xmlns="http://www.topografix.com/GPX/1/0">
↵ <rte>
↵ <name>undefined</name>
↵ <rtept lat="50.70373" lon="-3.07241" />
↵ <rtept lat="50.70348" lon="-3.07237" />
↵ </rte>
↵</gpx>"
__proto__: Object
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit:
ƒ}
message: "Http failure during parsing for http://localhost:3000/download/"
name: "HttpErrorResponse"
ok: false
status: 200
statusText: "OK"
url: "http://localhost:3000/download/"
__proto__: HttpResponseBase
The issue was that my download link did not include the http:// element of the url, as this answer describes.

Can't understand WebAuthn API error from JavaScript

I am currently building out an AJAX registration endpoint for Django to allow for FIDO2 authentication (physical hardware key login). This is from following the example/documentation from Yubico's official fido2 python library.
The only dependencies are cbor.js and js-cookie. Everything server-side is working for now, however, I keep getting this JavaScript error while invoking the navigator.credentials.create method
TypeError: Failed to execute 'create' on
'CredentialsContainer': The provided value is not of
type '(ArrayBuffer or ArrayBufferView)'
The code:
var csrftoken = Cookies.get('csrftoken');
fetch('/register/begin', {
method: 'POST',
headers: {
'X-CSRFToken': csrftoken
}
}).then(function(response) {
if(response.ok) {
return response.arrayBuffer();
}
throw new Error('Error getting registration data!');
}).then(CBOR.decode).then(function(options) {
console.log(options)
//This line is not working
return navigator.credentials.create(options);
//More code... complete registration...
I can't figure this out. Do you know whats wrong? Thanks!
I had the same problem, and the cause was that some of the pieces of data sent from the server in the response from /register/begin must be formatted as byte strings rather than unicode strings. In particular, I've found that the user_id and the credential ids have to be byte strings - assuming that you are also following Yubico's example in the server, implemented in python 3.
Also of note is that in this case I've found Firefox's error messages much more helpful than chome's.
I was having this issue as well. I ended up using the TextEncoder class to encode the challenge and the user id...
const enc = new TextEncoder();
const createCredentialOptions: CredentialCreationOptions = {
publicKey: {
rp: rp,
challenge: enc.encode(challenge),
user: {
id: enc.encode(user.id),
name: user.name,
displayName: user.displayName
},
pubKeyCredParams: pubKeyCredParams,
...

Swagger Nodejs - Response Validation Failed

I am building the API with Swagger and NodeJS, the annoying problem I have faced so far is Swagger validates the response, and it's not always smooth.
My case:
In file .yaml: I checked the yaml syntax with Swagger Editor => File Yaml is correct.
/user/createNew:
x-swagger-router-controller: xxxxxxx
post:
tags:
- User
summary: Create New User
# used as the method name of the controller
operationId: createNewUser
parameters:
- name: NewUserReq
in: body
required: true
description: Email register
schema:
$ref: "#/definitions/NewUserReq"
responses:
"201":
description: Successful
schema:
# a pointer to a definition
$ref: "#/definitions/CreateUserResp"
# responses may fall through to errors
default:
description: Error
schema:
$ref: "#/definitions/ErrorResponse"
CreateUserResp:
properties:
status:
type: integer
description: Response status
response:
$ref: "#/definitions/MsgResponse"
MsgResponse:
required:
- resp_msg
properties:
resp_msg:
type: string
To check the response format, I generated the NodeJs file from Swagger Editor
examples['application/json'] = {
"response" : {
"resp_msg" : "aeiou"
},
"status" : 123
};
In controller file .js:
function createNewUser(req,res){
....
var resp = new Object();
resp.resp_msg=data.email;
final_response.status = 200;
final_response.response = resp;
console.log("createNewUser::Query succeffully", JSON.stringify(final_response));
//{"status":200,"response":{"resp_msg":"test#gmail.com"}}
res.set('Content-Type', 'application/json');
res.json(final_response);
}
Try to run API with Postman, the error happens with log below:
Error: Response validation failed: failed schema validation
at throwErrorWithCode (/var/app/current/node_modules/swagger-express-mw/node_modules/swagger-node-runner/node_modules/swagger-tools/lib/validators.js:121:13)
at Object.module.exports.validateAgainstSchema (/var/app/current/node_modules/swagger-express-mw/node_modules/swagger-node-runner/node_modules/swagger-tools/lib/validators.js:176:7)
at /var/app/current/node_modules/swagger-express-mw/node_modules/swagger-node-runner/node_modules/swagger-tools/middleware/swagger-validator.js:141:22
at /var/app/current/node_modules/swagger-express-mw/node_modules/swagger-node-runner/node_modules/swagger-tools/node_modules/async/lib/async.js:356:13
at async.forEachOf.async.eachOf (/var/app/current/node_modules/swagger-express-mw/node_modules/swagger-node-runner/node_modules/swagger-tools/node_modules/async/lib/async.js:233:13)
at _asyncMap (/var/app/current/node_modules/swagger-express-mw/node_modules/swagger-node-runner/node_modules/swagger-tools/node_modules/async/lib/async.js:355:9)
at Object.map (/var/app/current/node_modules/swagger-express-mw/node_modules/swagger-node-runner/node_modules/swagger-tools/node_modules/async/lib/async.js:337:20)
at validateValue (/var/app/current/node_modules/swagger-express-mw/node_modules/swagger-node-runner/node_modules/swagger-tools/middleware/swagger-validator.js:134:11)
at ServerResponse.res.end (/var/app/current/node_modules/swagger-express-mw/node_modules/swagger-node-runner/node_modules/swagger-tools/middleware/swagger-validator.js:252:9)
at ServerResponse.send (/var/app/current/node_modules/express/lib/response.js:205:10)
I cannot figure out what caused the error, I double checked the structure of JSON response.
Very appreciate for any suggestion.
You have put required: true which means you have to pass that parameter otherwise Response Validation Failed will appear.
I tried to reproduce but it gives me some errors. However, I tried removing MsgResponse and defining CreateUserResp as a single definition, and it worked:
CreateUserResp:
type: object
properties:
response:
type: object
properties:
resp_msg:
type: string
description: The response object
status:
type: number
description: The status code
It seems like you're not defining response as an object, and just adding some properties to it.
After trying some experiments, I made it work. My solution simply is changing response code "201" to "200" in yaml file, and it worked. I don't know why I left the response code "201". Anyway, I am still new with this, and don't know if it's a best practice or not. I am open to receive any better suggestion.
You can get this error if you don't specify in the header the data type sent within the request body
Content-Type : application/json

Express.js: API URL and res.json, using request package from npm

I'm at wits end trying to figure out how to get my API to return a valid JSON object.
I'm currently using the following code to send a response:
res.json({ foo: 'bar', secret: 'code' });
I have also tried:
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify({ foo: 'bar', secret: 'code' }));
Both of the above methods work when I visit the API URL in browser. I get the expected:
{
"foo": "bar",
"secret": "code"
}
On the other hand, I'm having trouble parsing the JSON response from URL (the example below uses the request package from npm):
request({
url: API-URL,
json: true
}, function (error, response, body) {
if (!error) {
console.log(body);
} else {
console.log(error);
}
})
Using the above code, I get the following error response:
{
"code": "ENOTFOUND",
"errno": "ENOTFOUND",
"syscall": "getaddrinfo",
"hostname": <<Redacted>>,
"host": <<Redacted>>,
"port": 443
}
I've tried plugging in third-party API URLs and they work just fine (ie: Google Maps, and OpenWeatherAPI) so I'm pretty sure that I'm using request properly. I'm guessing there is a step I missed in setting up the API (or I'm doing something wrong)?
Testing my API URL in https://jsonformatter.curiousconcept.com/, the formatted JSON object shows up but I also get the following regardless of how I set the response header and encoding:
Error: Invalid encoding, expecting UTF-8, UTF-16 or UTF-32.[Code 29, Structure 0]
Any suggestions would be greatly appreciated!
Edit:
The above is in a live web app and Express works properly otherwise (everything else in the application works fine, the JSON response just isn't playing nicely with request or jQuery.

Categories