Node.js My XML is not reading my XSLT - node.js

My Node.js follows this format.
if(req.method =="POST"){ refineXML(req, result =>{
res.writeHead(200, {'Context-Type': 'text/xml'});
res.write(result);
res.end();
});
};
The refineXML code is a simple method that takes in an encoded xml string, decodes it, removes the id, and then sends it back to the user. When I console.log the result, the answer is in promper xml format. However, I can't figure out how to link it to my XSL document. My XSL document currently lies under my project->WebContent->XSL
My Console keeps printing out
TypeError: Cannot read property 'Symbol(asyncId)' of null
whatever that means while my firefox says
Error Loading stylesheet:The TLS handshake finished for ...
http://localhost:3000/my.xsl

What does the stylesheet declaration look like in your xml document?
should look something like:
<?xml-stylesheet href="my.xsl"?>
the href should be a relative path so if your xml is at my project->WebContent then the PI would be:
<?xml-stylesheet href="./XSL/my.xsl"?>
technically the leading ./ isn't needed but I find it aids clarity

Related

How to create malformed XML (SOAP request) in nodejs

I have been given a task to generate malformed SOAP Request and checking for what kind of error response I receive.
For example:
const payload = `
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:test="urn:TEST"
>
<soap:Body>
<test:GetOperatingDataValues
xmlns:test="urn:TEST"
>
<test:ListId>1</test:ListId>
</test:GetOperatingDataValues>
</soap:Body>
</soap:Envelope>`
Now, if I send the above request body to the backend server like :
const result = axios.post("backendServer", payload)
console.log(result) will give the response from backend (in this case, response from GetOperatingDataValues function for listId 1)
Now, I have to modify that payload in different ways like, removing the end tag, different opening and closing tag names, deleting '<' from any tag, or removing '/' from end tags, different method name, etc and send that payload and check for the error response.
Is there any way to modify the correctly formed xml to malformed xml (in node js).
I have gone through different packages such as xml2js, fast-xml-parser. But these packages just form the correct xml.
Any help would be highly appreciated.
I would have thought it fairly obvious that to create a non-XML file, you don't want to use an XML tool. Just treat the XML as a character string and apply a regular expression, for example replacing </ by <.

Handling UTF8 characters in express route parameters

I'm having an issue with a NodeJS REST api created using express.
I have two calls, a get and a post set up like this:
router.get('/:id', (request, response) => {
console.log(request.params.id);
});
router.post('/:id', (request, response) => {
console.log(request.params.id);
});
now, I want the ID to be able to contain special characters (UTF8).
The problem is, when I use postman to test the requests, it looks like they are encoded very differently:
GET http://localhost:3000/api/â outputs â
POST http://localhost:3000/api/â outputs â
Does anyone have any idea what I am missing here?
I must mention that the post call also contains a file upload so the content type will be multipart/form-data
You should encode your URL on the client and decode it on the server. See the following articles:
What is the proper way to URL encode Unicode characters?
Can urls have UTF-8 characters?
Which characters make a URL invalid?
For JavaScript, encodeURI may come in handy.
It looks like postman does UTF-8 encoding but NOT proper url encoding. Consequently, what you type in the request url box translates to something different than what would happen if you typed that url in a browser.
I'm requesting: GET localhost/ä but it encodes it on the wire as localhost/ä
(This is now an invalid URL because it contains non ascii characters)
But when I type localhost/ä in to google chrome, it correctly encodes the request as localhost/%C3%A4
So you could try manually url encoding your request to http://localhost:3000/api/%C3%A2
In my opinion this is a bug (perhaps a regression). I am using the latest version of PostMan v7.11.0 on MacOS.
Does anyone have any idea what I am missing here?
yeah, it doesn't output â, it outputs â, but whatever you're checking the result with, think you're reading something else (iso-8859-1 maybe?), not UTF-8, and renders it as â
Most likely, you're viewing the result in a web browser, and the web server is sending the wrong Content-Type header. try doing header("Content-type: text/plain;charset=utf-8"); or header("Content-type: text/html;charset=utf-8"); , then your browser should render your â correctly.

Render raw html in response with Express

I would like to know how to render a raw HTML string in a response with Express.
My question is different from the others because I am not trying to render a raw HTML template; rather I just want to render a single raw HTML string.
Here is what I have tried in my route file.
router.get('/myRoute', function (req, res, next) {
var someHTML = "bar"
res.send(someHTML);
});
But when I point my browser to this route, I see a hyperlink, instead of a raw HTML string. I have tried to set the content-type to text by doing: res.setHeader('Content-Type', 'text'); with no avail.
Any suggestions?
For others arriving here; this worked best for me:
res.set('Content-Type', 'text/html');
res.send(Buffer.from('<h2>Test String</h2>'));
Edit:
And if your issue is escaping certain characters, then try using template literals: Template literals
The best way to do this is, assuming you're using callback style, declare var output=""....then go through appending what you need to the output var with +=.... use a template literal (new line doesn't matter ${variables in here}) if it's a large string... then res.writeHead(200,{Content-Type: text/html); res.end(output)
Encode the HTML before sending it. Someone made a Gist for this: https://gist.github.com/mikedeboer/1aa7cd2bbcb8e0abc16a
Just add tags around it
someHTML = "<plaintext>" + someHTML + "</plaintext>";
Just a word of caution that the plaintext is considered obsolete which means browser vendors have no obligation to implement them. However ,it still works on major browsers.
Another way you could do it is
someHTML = someHTML.replace(/</g, '<').replace(/>/g, '>');

converting RIchText to Mime in java using domino designer

Hi guys i am working Inline attachments(like images inside a mail body) in lotus notes.Based on the guidance on this question(can anyone please tell how to deal with inline images in lotus notes am trying to convert the Mail body from Richtext to MIME using.Domino version is 8.5.3
m_session.setConvertMIME(false);
doc.removeItem("$KeepPrivate");
doc.convertToMIME(doc.CVT_RT_TO_HTML,0);
MIMEEntity me=doc.getMIMEEntity("body");
The code is working fine when tried with agent.But when deployed in the server i am getting
NotesException: Conversion To MIME Failed:
[1FD8:0047-1DDC] 12-02-2014 18:30:23 HTTP JVM: HTMLAPI Problem converting to HTML.
When i searched all the material is saying this is a problem with lotus notes.can anyone please tell how to fix this.or is there any work around for this.Please help
I got a working RichText > Mime conversion script in ssjs, I guess this could be adapted to Java
function convertBodyToMimeAndSave(documentToConvert){
// Create a temporary document
// Calling convertToMime makes a MIME output of the full
/// document and puts it in body. If you have other
// fields than body in the original document, that
// will produce undesirable added content to the body
var tmp = database.createDocument();
// Put the original richtext in it
var rt = targetDocument.getFirstItem("Body")
if(!rt) return targetDocument
rt.copyItemToDocument(tmp)
// Convert the temporary document to MIME
tmp.convertToMIME(2)
// Copy all Items (that is, the Body) back to the original document
// (copying the Body specifically seemed to make the script crash)
tmp.copyAllItems (targetDocument, true)
targetDocument.closeMIMEEntities(true, "Body")
targetDocument.save()
}

nodejs/mocha/nock - mocking an entire html response

How can I mock an entire HTML body response for my tests?
I'm using nodejs/mocha/nock.
With nock I can mock JSON responds just fine, for example:
nock('http://myapp.iriscouch.com')
.get('/users/1')
.reply(200, {_id: "123ABC", _rev: "946B7D1C", username: 'pgte'});
I used curl -o to fetch the html I want for the mock, so I have it already in a file - but I don't see how can I pass an HTML file to nock (or something else).
Thanks.
First fetch the HTML content of your test file and put it in a string (using fs.readFile for example)
after that you can do:
nock('http://myapp.iriscouch.com').
get('/users/1').
reply(200, yourFileContent);
This is what worked out for me in the past :)
If you'd like, you can specify the content type explicitly, since you specify the body as a string this will effectively let you mock any non-binary response easily:
nock('http://myapp.iriscouch.com').
get('/users/1').
reply(200, yourFileContent, {'content-type': 'text/html'});
If you want a more general approach, I've asked a more general question about a similar issue and got some interesting responses.

Resources