(GET) API Request Failed in Javascript - javascript

I'm trying to fetch API data from antares (IoT Platform) using native javascript, here's the code
var url = 'https://platform.antares.id:8443/~/antares-cse/antares-id/SuhuPagarsih/Temperature/la';
var opt = {
method: 'GET',
mode: 'no-cors',
headers: {
'X-M2M-Origin': 'bdc2996719c6ac07:8cbb60e9c5997e74',
'Content-Type': 'application/json;ty=4',
'Accept':'application/json',
},
}
fetch(url, opt)
.then(function(data) {
console.log(data)
})
.catch(function(err) {
console.error(err)
})
Here's the result
as you can see, it's failed. Most of the time I see the error reason is forbidden. But when I try using node js and snippet code from postman. I get a good result (200 OK) Here's the code if you need to compare (I guess it's all the same)
var http = require("https");
var options = {
"method": "GET",
"hostname": "platform.antares.id",
"port": "8443",
"path": "/~/antares-cse/antares-id/SuhuPagarsih/Temperature/la",
"headers": {
"x-m2m-origin": "bdc2996719c6ac07:8cbb60e9c5997e74",
"content-type": "application/json;ty=3",
"cache-control": "no-cache",
"postman-token": "ff6cbe83-97ac-6ed5-aaec-e2bafaf44002"
}
};
var req = http.request(options, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
var body = Buffer.concat(chunks);
console.log(body.toString());
});
});
req.end();
And here's the result
My questions is, what seems to be the problem? what's the difference between making API request from native javascript and node js? and how can I resolve this problem?
Or maybe is there anyway around this issues? My goal is only to show the data from API to HTML/Web Page
Thank you!

Related

How to save HTTPS post response to global variable in Node

I am trying to integrate with a payment gateway in Node and I have successfully been able to post data, but I need to grab the HTTP response status code and store it in a global variable in order to use it for validation.
_doRequest(postData) {
const hostName = XXXXXXXXXXXXXXX;
const path = '/api/transact.php';
postData.security_key = this.security_key;
postData = querystring.stringify(postData);
const options = {
hostname: hostName,
path: path,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(postData)
}
};
// Make request to Direct Post API
const req = https.request(options, (response) => {
console.log(`STATUS: ${response.statusCode}`);
console.log(`HEADERS: ${JSON.stringify(response.headers)}`);
response.on('data', (chunk) => {
console.log(`BODY: ${chunk}`);
});
response.on('end', () => {
console.log('No more data in response.');
});
});
req.on('error', (e) => {
console.error(`Problem with request: ${e.message}`);
});
response.statusCode is what I'm looking to hoist somehow and store in a global variable like const status = statusCode, but I cannot figure out how to accomplish this. I would appreciate any help!
Please try using global.status = statusCode.
You can always access it, while node is running, however this is considered an antipattern.

Returning from an API request [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 3 years ago.
I have a function that does POST request to get access tokens from an API. The function works insofar as printing the token to the console but I couldn't find a way to parse and save the access-token to a variable for using in other methods.
I've searched a lot about these on Stack Overflow, and one thing I came across was the notion of callbacks, async/await... However there is no concrete answers that show me how to RETURN the value after the request is made.
I'm getting undefined values, probably because my function is executing too fast before the request ends but nonetheless I would like to know a specific code implementation (using another callback function or something different) that allows me to get the value accessible outside these asynchronous functions.
My code is below along with a screenshot. Keep in mind this is a Lambda code for an Alexa skill
function ParseToken(obj) {
var string = obj + "";
return string;
}
function httpRetrieveToken() {
var http = require("https");
var options = {
"method": "POST",
"host": "hostName",
"path": "pathVal",
"headers": {
"content-type": "contentType",
"Authorization": "Bearer token",
"cache-control": "no-cache",
}
};
var req = http.request(options, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
var body = Buffer.concat(chunks);
console.log(body.toString());
});
});
req.write("stuff here");
req.end();
}
Parsing token
Once you have the text representing your token object you can use JSON.parse to convert it to an object:
var json = '{"token":"ABCDEFG123456789", "expires":36000}';
obj = JSON.parse(json);
console.log(obj.token);
// expected output: ABCDEFG123456789
Using Callback
You can send a callback function as parameter to the httpRetrieveToken function like:
var http = require("https");
function httpRetrieveToken(cb) {
var options = {
"method": "POST",
"host": "hostName",
"path": "pathVal",
"headers": {
"content-type": "contentType",
"Authorization": "Bearer token",
"cache-control": "no-cache",
}
};
var req = http.request(options, function (res) {
var _cb = cb;
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
var body = Buffer.concat(chunks);
var token = JSON.parse(body.toString());
_cb(token); // Callback function
});
});
req.write("stuff here");
req.end();
}
Then you can use function like:
httpRetrieveToken(function(token) {
// Do something with the token
});
The reason to use a callback function is to execute it once the asynchronous process has been finished, then enclose in that callback the processes to follow with the required data.
Using Promise
Using promise object, you provide a function who executes either resolve when your process was successful or reject when you had an error. Then you set a function to be executed on sucess with then method, where you can use the token retrived by your http request.
var http = require("https");
function httpRetrieveToken() {
var options = {
"method": "POST",
"host": "hostName",
"path": "pathVal",
"headers": {
"content-type": "contentType",
"Authorization": "Bearer token",
"cache-control": "no-cache",
}
};
return new Promise(function(resolve, reject) {
let req = http.request(options, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
var body = Buffer.concat(chunks);
var token = JSON.parse(body.toString());
resolve(null, token); // Callback function
});
});
req.on("error", function(err) {
reject(err);
});
});
}
httpRetrieveToken().then((err, token) => {
if(err) console.log(err);
// Do something with token
});

net.request post data not working

Posting data using net.request is not working. It's reaching the URL. But data not posted. My code is below. Please help me on this.
const net = remote.net;
const querystring = require('querystring');
//**
var postData = querystring.stringify({
'username' : 'test',
'password': 'test'
});
const request = net.request({
method: 'POST',
url: 'http://127.0.0.1/post.php',
});
request.on('error', (error) => {});
request.on('response', (response) => {});
request.write(postData);
request.end();
I know it's been a while. But for the next people who will have the same problem.
Don't forget you must be declare the size of your "postData" in the header.
for example :
var postData = JSON.stringify({"q" : sqlQuery });
const request = net.request({
method: 'POST',
protocol: 'http:',
hostname: '127.0.0.1',
port: 3000,
path: '/select',
headers: {
'Content-Type': 'application/json',
'Content-Length': postData.length
}
})
request.on('response', (response) => {
.... // Something
})
request.write(postData)
request.end()
When using net.request you need to call request.end() after you write your data to assure the request data has been sent.
The reason you need to call request.end() is to allow for changes to be made to the headers and body of your request before you actually make it.
request() will connect and wait for request.end() to send the headers and body in one pass.
Also, it is never a good idea to ignore responses if you want to understand what your code is doing.
You really should hook the request.response event to see what, if any, errors occurred, as in:
request.on('response', (response) => {
console.log(`STATUS: ${response.statusCode}`)
console.log(`HEADERS: ${JSON.stringify(response.headers)}`)
})
Updated - per comment
Your code should read (expanded from example code for net in Electron API Docs):
const request = net.request({
method: 'POST',
url: 'http://127.0.0.1/post.php',
})
let body = ''
request.on('response', (response) => {
// check response.statusCode to determine if the request succeeded
console.log(`STATUS: ${response.statusCode}`)
console.log(`HEADERS: ${JSON.stringify(response.headers)}`)
// capture body of response
// - can be called more than once for large result
response.on('data', (chunk) => {
console.log(`BODY: ${chunk}`)
body += chunk.toString()
})
// when response is complete, print body
response.on('end', () => {
console.log(`BODY: ${body}`)
})
})
request.write(postData)
request.end()

Query params with special characters in nodejs

My URL:
http://abcde.com/result.aspx?q=(some%20name%20here%20):Done&p=1&pos=0&qType=2
Its working in postman and also in curl command. If i try this in nodejs code
var http = require("http");
var options = {
"method": "GET",
"hostname": "abcde.com",
"port": null,
"path": "/result.aspx?q=(some%2520name%2520here%2520)%3ADone&p=1&pos=0&qType=2",
"headers": {
"cache-control": "no-cache",
"postman-token": "8e755cba-5d19-446f-269e-e880bd8cd7e1"
}
};
var req = http.request(options, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
var body = Buffer.concat(chunks);
console.log(body.toString());
});
});
req.end();
Its not working. I think, problem is some special characters in query params. can anyone know how to fix this issue.

Post Method works using https.request but not with Needle or Request libraries

I have nodeApp. It does stuff.
At a particular time I need to communicate with an API that out there in the wild. Using the API in rest tool like Postman is straight forward:
Postman
Url:
https://epicurl
Headers:
Content-Type : application/json
Accept : application/json
x-key : secret
Body:
{
"some":"kickass"
"data":"here"
}
Sending the above in Postman I get a nice quick response! Yay for rest tools.
So their API works, now I need to make that same response in my Node.js application.
This is where things get odd...
Request Module: FAILS
var request = require('request')
...lots_of_other_stuff...
var options = {
uri: 'https://epicURL',
method: 'POST',
json: true,
headers : {
"Content-Type":"application/json",
"Accept":"application/json",
"x-key":"secretbro"
},
body : JSON.stringify(bodyModel)
};
request(options, function(error, response, body) {
if (!error) {
console.log('Body is:');
console.log(body);
} else {
console.log('Error is:');
logger.info(error);
}
cb(body); //Callback sends request back...
});
The above fails.. It throws the good'ol ECONNRESET error that we all love! Why? Who knows?
https.request() - WORKS!
var https = require("https");
https.globalAgent.options.secureProtocol = 'SSLv3_method';
var headers = {
"Content-Type":"application/json",
"Accept":"application/json",
"x-key":"nicetrybro"
}
var options = {
host: 'www.l33turls.com',
port:443,
path: "/sweetpathsofjebus",
method: 'POST',
headers: headers
};
var req = https.request(options, function(res) {
res.setEncoding('utf-8');
var responseString = '';
res.on('data', function(data) {
responseString += data;
});
res.on('end', function() {
var resultObject = responseString;
//Call the callback function to get this response object back to the router.
cb(resultObject);
});
});
req.on('error', function(e) {
console.log(e);
});
req.write(bodyString);
req.end();
But then I notice...
If i leave this line of code in place when using the Request Module it then works...
var https = require("https");
https.globalAgent.options.secureProtocol = 'SSLv3_method';
Is this documented somewhere and I am missing it? Anyone explain this to me?

Categories