NodeJS soap request "Invalid WSDL URL" - node.js

I'm trying to make a request to a web service with a URL similar to the following:
http://91.21.24.521:7831/XXXXX/WS/{companyName}/XXXXX/XXXX?wsdl
I've blanked out some information with the XXXX, but that's the basic structure of the URL. The company name actually has blank spaces. I've tried making the request with the blank spaces and filling them with %20, but neither work. The URL works fine in Soap UI, where I fill the blank spaces with %20.
In Javascript I'm using the "soap" NPM module. And my current code looks like this:
const auth = 'Basic ' + Buffer.from(`${user}:${password}`).toString('base64')
const options = {
wsdl_headers: {
'Authorization': auth,
forceSoap12Headers: true,
disableCache: true,
}
}
soap.createClient(url, options, function (err, client) {
console.log(err, client)
})
When I inspect the console log, I get an undefined client and an error that says Invalid WSDL URL.
I've tried removing the options, trying different version of the URL, changing the options object structure but nothing seems to work. I'me confused because the URL works fine in Soap UI and the web browswer, but doesn't work in this scenario. Is there something wrong with the code?
Thanks

Related

Cookie not being set from node typescript request

I'm trying to set a cookie in a node request. I have tried using packages like js-cookie, cookie-js, cookie and cookie-manager but none work.
The way I have tried it is very straight-forward, whenever my endpoint gets called i.e. https://develop.api/sess/init, I set the cookie at the very beggining of the endpoint with the following code
import * as Cookies from 'js-cookie';
export const init = async (event: APIGatewayEvent, context: Context) => {
...
Cookies.set('hello', 'hello');
...
}
As my endpoint has an auth header, I can not directly call it into my browser URL due to missing permissions, so I tried generating the fetch function with postman and pasting it into my browser's console. The function is the following
var myHeaders = new Headers();
myHeaders.append("Referer", "accepted.referer.com");
myHeaders.append("key", "somekey");
var requestOptions = {
method: 'GET',
headers: myHeaders,
redirect: 'follow'
};
fetch("https://develop.api/sess/init", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
Once called, my request successfully returns the expected response, but it never shows a Set-Cookie header in the network section, neither shows my cookie in the Application section.
I have to mention that I also tried looking for the cookie when making the call within Postman, but it never sets it neither.
Also, I have tried starting the application in localhost, and I have a successful response, but my cookie is still not being set.
About the package showed in the code, I said I have tried it with different ones and their implementations, so I don't think a broken package is the problem.
I'm starting to think that I have a wrong idea about how cookies work, or that someway I am completely blocking the sending of cookies within my code.
Environment
If it helps in any way, my endpoint is being hosted in a AWS Lambda application.
I know this should be trivial, but being battling with it for a day now.
I finally answered my own issue. The key here is that I'm using AWS lambdas as the proxy, therefore, the headers I were using to send the cookies were wrong, I was sending the cookies with the endpoint instead of within the lambda. Let me explain myself.
I was adding 'Set-Cookie':'cookieKey:cookieVal' in the headers of the Postman Call that I was using to test both my local and develop environments.
Instead of that, I needed to send the request within the response of the lambda for the cookies to be registered.
Please check at the following links for similar cases ->
https://aws.amazon.com/blogs/compute/simply-serverless-using-aws-lambda-to-expose-custom-cookies-with-api-gateway/
https://forum.serverless.com/t/how-to-send-a-cookie-as-a-response/1312/7

Invalid credentials problem when consumig tone analizer from a nodejs app

I'm trying to consume a tone analyzer service from a nodejs app. I get unauthorized access problem, but these credentials work fine when I use them in a curl.
Running locally, in my app.js file I've included the data of the tone analyzer as follows:
var ToneAnalyzerV3 = require('watson-developer-cloud/tone-analyzer/v3');
var toneAnalyzer = new ToneAnalyzerV3({
version: '2017-09-21',
iam_apikey: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
});
Then I've added this, so my app listens for post requestes in the /tone url:
app.post('/tone', function(req, res, next) {
var params = {'tone_input': req.body}
toneAnalyzer.tone(params, function(err, data) {
if (err) {
return next(err);
}
return res.json(data);
});
});
But when I call it I get "Unauthorized: Access is denied due to invalid credentials".
The thing is that these credentials work fine in curl:
curl -X POST -u "apikey:XXXXXXXXXXXXXXXXXXXXXXXXXXXXX" --header "Content-Type: application/json" --data-binary #tone.json "https://gateway-lon.watsonplatform.net/tone-analyzer/api/v3/tone?version=2017-09-21&sentences=false"
{"document_tone":{"tones":[{"score":0.6165,"tone_id":"sadness","tone_name":"Sadness"},{"score":0.829888,"tone_id":"analytical","tone_name":"Analytical"}]}}
The reason you are getting unauthorised errors when running locally is that your service is hosted in https://gateway-lon.watsonplatform.net. If you don't specify an endpoint / url in your ToneAnalyzerV3 constructor then the API / SDK defaults to Dallas. So although your credentials may be correct for London, they are not correct for Dallas.
When you deployed your app to the cloud (which I guess was to the London location), you probably bound the service into your application. This sets environment variables allowing the SDK to determine the endpoint.
You constructor should look like:
var toneAnalyzer = new ToneAnalyzerV3({
version: '2017-09-21',
iam_apikey: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
url: 'https://gateway-lon.watsonplatform.net/tone-analyzer/api',
});
I don't see problem with code (also never used watson stuff), but you may check the following point :
How the request you really send is formated : Because I see that you send param that is not present in your curl request.
Is your function using POST aswell (you don't provide much detail on what the call to toneAnalyzer.tone does exactly) ? Maybe it's a conflict of headers or Content-Type.
Do you use a proxy (enterprise settings or stuff like that) ? If you do, you may check that node is correctly using it.
You should also provide a bit more details on what exactly your tone object do, and try to find where the call to the IBM API is done.

500 error depending on data submitted in POST request

Setup: NodeJS / Express / Helmet API running on Azure App Service, CORS set to allow * so should be all fine.
Client web setup running locally on NodeJS (http://localhost:3000)
I'm trying to POST some JSON from the web client to the API for login validating purposes. Works for some requests and not others, seemingly depending on what actual values I am posting.
I've got a bit of code that uses the Fetch API to send login details and validate them, as follows:
const response = await fetch(`${baseUrl}/auth/validate`, {
method: 'POST',
mode: 'cors',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({EmailAddress: email, Password: password}),
}).catch(error => {
alert('Unable to check login');
});
If the value of email and password are both just me leaning on the keyboard, say 'skdjflsjdf' then it's all fine - the server gets my POST, validates that the login is rubbish and bounces back my error. It allows it because I get the Access-Control-Allow-Origin: * header back, so Chrome is happy.
If the value of email is an actual email address, I don't get the Access-Control-Allow-Origin: * header back and Chrome gets very upset of course.
I can't even begin to imagine how I've gone wrong here and I certainly don't seem to be able to find the right phrase to Google for a sensible answer. I'd really appreciate any help... Thanks.

How to add Test Case into TFS via REST

I'm implementing lib to export/synchronize test cases into TFS automaticaly during test run. One of my requirements is that I need to use NodeJS for that, so I decided to use TFS REST API. In my code I'm using "azure-devops-node-api " lib, I can connect and get different elements, no luck with adding test cases.
I've found on the web that TestCase is kind of WorkItem and as WI should be added. Unfortunately I didn't find a way to add one with azure-devops-node-api.
I tried also to send manually constructed json, unfortunatelly no luck with finding proper url to sent as I'm allways getting :
Error: {"statusCode":404,"body":"Page not found."
My example request:
request.post({
url: 'https://<url>/tfs/<default collection maybe?>/<project>/_apis/wit/workItems/test%20case',
headers: {
'Content-Type': 'application/json',
'Authorization':'Basic ' + this.encodePat('<my auth token>')
},
rejectUnauthorized: false,//add when working with https sites
requestCert: false,//add when working with https sites
agent: false,//add when working with https sites
json: rq
},function (response, err, body){
if (err) throw new Error(JSON.stringify(err));
console.log('Body:',JSON.parse(body));
console.log('Response:',response)
});
Does anyone know what wrom I'm doing or if azure-devops-node-api is able to add WorkItems ?
If you want to add a test case only just as a work item you have to use the template from this link Work Items - Create.
POST https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/${type}?api-version=5.0
You have to add to url "?api-version=X.0". The example:
https://<server name and port>/tfs/<collection name should be>/<project>/_apis/wit/workItems/$test%20case?api-version=3.0
Also you have to encode your pat by this template:
'Authorization':'Basic ' + this.encodePat(':<my auth token>')
Here you can find an example for a build task with node.js: https://github.com/ashamrai/AzureDevOpsExtensions/blob/master/CustomBuildTask/NewWICustomTask/index.ts

HTTP Request doesn’t include query

I am trying to send a POST request with query parameters using the request library. I have succeded in sending these requests, but I am still unable to include any working query.
I have followed instructions from other posts and concluded that I’ve tried everything on the internet that is related to my mission.
This is the code that does comply with what I found on the internet:
const request = require(’request’);
request.post({
url: 'https://example.com/endpoint',
qs: { with_counts: true } },
function (err, res, body) {
console.log(err, body);
}
);
This, however, does not work. The server does not recieve the query. I tried setting the body and json properties instead, but those didn’t work either.
I cant tell what I’m doing wrong, and I realize that it might not be possible for you to tell, but this is my last desperate attempt before giving up and switching to Python.

Resources