I am calling this public API from my nodejs. I am using system in office enviornment and I have to connect with VPN to accress office url. When I use wifi of my mobile then I can access below API.
https://dummy.restapiexample.com/api/v1/employees
the above is public API. anybody can check.
I have to use https to access API in node because of security reasons. How can i access the API when I am in my office enviornment. I am getting below error.
Error:connect ETIMEDOUT 52.220.246.189:443.
below is the code.
const https = require('https');
https.get('https://dummy.restapiexample.com/api/v1/employees', (resp) => {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
console.log(data);
});
resp.on('end', () => {
console.log(JSON.parse(data).explanation);
});
console.log("data");
}).on("error", (err) => {
console.log("Error: " + err.message);
});
How can I resolve the same?
Related
i have got the data by making the following http request , currently i am doing console.log() , but how to see the data in postman.
function getProducts() {
http
.get(url, (res) => {
let body = "";
res.on("data", (chunk) => {
body += chunk;
});
res.on("end", () => {
try {
let json = JSON.parse(body);
console.log(json);
} catch (error) {
console.error(error.message);
}
});
})
.on("error", (error) => {
console.error(error.message);
});
}
http.get() does exactly what Postman do.
If you want the data in Postman, just call url in it.
I'm not sure if I understand it correctly but here's what I can say
Download and install postman from here
The UI is simple, it has tabs like browsers, create a new tab(request)
Default http method is Get so you don't need to change it. type your URL click Send button.
you can add Headers, etc...
I have json file and created node.js server to set endpoint and then get this data via my React Native application. If I'm not wrong it worked correctly in friday but I had to mess something up and now I totally don't know how to fix it. All time I get error:
Possible Unhandled Promise Rejection (id: 0): TypeError: Network
request failed
self.fetch/http://192.168.1.39:8081/1c49a23b-7fbb-c640-a946-c1e001192c92:27859:18
dispatchEvent#blob:http://192.168.1.39:8081/1c49a23b-7fbb-c640-a946-c1e001192c92:29144:13
setReadyState#blob:http://192.168.1.39:8081/1c49a23b-7fbb-c640-a946-c1e001192c92:28897:15
__didCompleteResponse#blob:http://192.168.1.39:8081/1c49a23b-7fbb-c640-a946-c1e001192c92:28724:11
send/<#blob:http://192.168.1.39:8081/1c49a23b-7fbb-c640-a946-c1e001192c92:28834:18
emit#blob:http://192.168.1.39:8081/1c49a23b-7fbb-c640-a946-c1e001192c92:4538:15
__callFunction#blob:http://192.168.1.39:8081/1c49a23b-7fbb-c640-a946-c1e001192c92:2608:22
callFunctionReturnFlushedQueue/<#blob:http://192.168.1.39:8081/1c49a23b-7fbb-c640-a946-c1e001192c92:2385:11
__guard#blob:http://192.168.1.39:8081/1c49a23b-7fbb-c640-a946-c1e001192c92:2561:13
callFunctionReturnFlushedQueue#blob:http://192.168.1.39:8081/1c49a23b-7fbb-c640-a946-c1e001192c92:2384:9
onmessage#http://192.168.1.39:8081/debugger-ui/debuggerWorker.js:72:25
my Node server:
const filename = './logos.json';
const server = http.createServer((req, res) => {
if (req.url === "/logo") {
res.writeHead(200, { "Content-Type": "application/json" });
fs.createReadStream(__dirname + "/logos.json").pipe(res)
}
})
server.listen(3000, (err) => {
if (err) throw err;
console.log('server is listening on port 3000');
})
and my RN code :
syncLogoData = () => {
fetch('http://localhost:3000/logo')
.then(resp => resp.json())
.then(data => console.log(data))
.catch(err => console.log(err))
}
Looks like you're trying to run this on a device. The device doesn't know localhost points to your server. In your syncLogoData, change the uri to http://ip_address:3000/logo and it should work.
Also helpful to open http://localhost:3000/logo on your computer browser to make sure your server code is correct.
Below is the function i am calling but every time i am getting below error
Error - MalformedResponse
Failed to parse Dialogflow response into AppResponse because of empty speech response.
$ below is the complete code
function callExternalAPI () {
return new Promise((resolve, reject) => {
let path = 'path';
console.log('API Request: ' + path);
http.get({host: host, path: path}, (res) => {
let body = '';
res.on('data', (d) => { body += d; });
res.on('end', () => {
let response = JSON.parse(body);
let output = 'response';
console.log(output);
resolve(output);
});
res.on('error', (error) => {
console.log(`Error calling the weather API: ${error}`);
reject();
});
});
let intentMap = new Map();
intentMap.set('CardView',callExternalAPI);
agent.handleRequest(intentMap);
});
The inline editor uses Cloud Functions for Firebase. By default, your project is using the Firebase "Spark" plan, which limits network connections to Google services only.
Since the connection is being rejected, the if (err) block is being triggered, and you have not specified a response to be sent back to the user when this happens, so you're getting the "empty speech response" error.
The easiest solution is to upgrade to paid plan, such as the "Blaze" plan, which will require you to register a credit card, but which has a free tier, so you won't be charged for a base level of operations, which generally covers your development and testing, and may even cover light production usage.
You should likely also setup a response in the event of an error.
When accessing WSDL api via another tool it is working but when i try to create a client via node it gives this error.
{ [Error: Parse Error] bytesParsed: 161, code:
'HPE_INVALID_HEADER_TOKEN' }
Code i am using
var url = 'https://payments.jazzcash.com.pk/PayAxisExternalStatusService/StatusService_v11.svc?wsdl';
soap.createClient(url, function(err, client) {
console.log(err);
console.log(client); })
Using node module soap
You might need to use https://www.npmjs.com/package/http-parser-js
1 npm install http-parser-js
2 Insert this code before require('soap')
process.binding('http_parser').HTTPParser = require('http-parser-js').HTTPParser;
Following above steps will fix your issue
Node.js is really strict about response format of the server.
I tried http-parser-js but it is very sensitive to the version of Node.js you use.
If you need to communicate with the server which sends malformed responses the only way I see is to use sockets:
const net = require('net');
const socketConnection = net.createConnection('80', 'google.com');
socketConnection.on('data', (data) => {
console.log('SOCKET RESPONSE', data.toString());
}).on('connect', () => {
const request = "GET / HTTP/1.1\r\n"
+ "Accept: */*\r\n\r\n";
socketConnection.write(request);
console.log('request sent');
}).on('end', () => {
console.log('the end');
}).on('error', (error) => {
console.log('connection error:', error);
});
In the context of the SOAP client, you can get WSDL yourself and store it locally and then create a SOAP client.
In WAMS, Windows Azure Mobile Service, you could easily configure a Schedule Service to call on any custom API that you have. However, how is that same scenario being implemented with Azure Web Jobs in Node ? How can I access the reference to an Azure Mobile Service Client object that I can use to call an EasyAPI Service from a Mobile Apps Module, like:
var client = azure.azureMobile;
azure.invokeApi("customers", {
method: "get"
}).done(function (results) {
console.log('Result:'+results.result);
}, function (error) {
console.log('Error:'+error);
});
Is there any available "azure"-like object that we can use from inside the WebJob code ?
As the Custom APis in Mobile Apps are exposed as RESTful APis, so you can simply implement HTTP requests against your Easy APIs on Mobile Apps. You can try to following code snippet in the webjob.
var http = require("https")
var options = {
host: "<mobileappname>.azurewebsites.net",
path: "/api/easyapi",
headers: {
"ZUMO-API-VERSION": "2.0.0"
}
};
req = http.request(options, (res)=>{
console.log(`STATUS: ${res.statusCode}`);
console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
res.on('data', (chunk) => {
console.log(`BODY: ${chunk}`);
});
res.on('end', () => {
console.log('No more data in response.');
});
});
req.on('error', (e) => {
console.log(`problem with request: ${e.message}`);
});
req.end();
Any further concern, please feel free to let me know.