Testing HTTP requests using Axios in JavaScipt Code Node as I need to implement NTLM Auth flow that HTTP Request does not support.
I can log the values to the Console.Log inside the axios.get(), but can’t get the values out to return them for the next task.
I tried declaring a variable outside and then assigning it inside to be able to send it, but the value never changes from the initial declaration:
let returnLocation = ‘TempString’;
…
return [{json:{‘locationUrl’: returnLocation}}]; //still has the value “TempString”
What is the error message (if any)?
It’s just not working as expected.
The request works and the correct value is populated in the ‘locationUrl’ nd can be seen on the Console.Log.
This part does not seem to assign the value to the outside variable:
returnLocation = locationUrl;
Please share your workflow
Share the output returned by the last node
Current Output:
[
{
“locationUrl”: “TempString”
}
]
It is tricky to test this but if it is still returning TempString there is likely going to be an issue in the code somewhere, I noticed when testing against webhook.site I am getting the temp response as I don’t have a response.headers.location coming back but it isn’t triggering the error.
Looking at what you are doing you might be able to do this using an HTTP Request node as well with Custom Auth and adding your header values there then calling your endpoint with the HTTP Request node and working with the redirect there but from what I understand doing this in the code node or with an http request won’t implement NTLM unless you are going to build out the entire process which could get messy.
What I would do is make a custom docker image and install axios-ntlm and see if that makes life easier.
FROM n8nio/n8n:latest
USER root
RUN npm install -g axios-ntlm
USER node
The Code works fine when run directly on Node.JS. Will put it below. The Get does work on my Node as the URL is output to the Console.log, just not assigned to the ‘returnLocation’ variable.
I have 4 functions in the series of requests that would become a node each and it might be true that some of them could be normal HTTP request nodes on the platform. They are just code now as I wrote the code in a NodeJS project in Visual Studio to test each step.
For the NTLM part after this step, I used: const httpntlm = require(‘httpntlm’);
that is not present here As I was first testing the get URL part for that step.
Code the Node is based on:
const axios = require('axios');
/**
* Initial GET Request:
* Make the initial GET request and capture the Location header from the 302 response.
*/
const headers = {
'X-Qlik-Xrfkey': '1234567890123456',
'X-Qlik-User': 'UserDirectory=BADGERHOLDINGS; UserId=riaandt',
'User-Agent': 'Windows',
'Cookie': ''
};
let returnLocation = 'TempString';
axios.get('https://smartreports.badgerholdings.co.za/qrs/about?Xrfkey=1234567890123456', {
headers: headers,
maxRedirects: 0,
validateStatus: function (status) {
// Accept only status codes less than 300
return status < 300;
}
})
.then(response => {
// Handle redirect URL
// For Return Flow where I already have a session Cookie
const locationUrl = response.headers.location;
returnLocation = locationUrl.toString();
console.log('#TextOut: ' + 'RediRectUrl ' + JSON.stringify(response.data) + ' #TextOut End');
})
.catch(error => {
if (error.response && error.response.status === 302) {
const locationUrl = error.response.headers.location;
console.log('#SessionErrorTextOut: ' + locationUrl + ' #SessionErrorTextOut End');
// Handle login redirect URL E.g. https://smartreports.badgerholdings.co.za/internal_windows_authentication/?targetId=af01e22d-e183-4fe3-aa0d-e255b0fdba07
returnLocation = locationUrl.toString();
} else {
console.error('Error:', error);
}
});
console.log('#SessionErrorTextOut: ' + returnLocation + ' #SessionErrorTextOut End');