We are trying to get node to use a ssl based api on another machine and it's failing with a socket hang up error. I'm putting the code below. Basically, on one machine we have a asp.net web api based api set up to accept a request, and on another we have a tomcat spring mvc based api accepting requests. Both apis work with fiddler, postman, other sites sending identical requests, however with node sending to the tomcat site, we get an immediate socket hangup error. We're really struggling trying to figure out what is wrong. So it will work correctly with one ssl based url, but fail with the other, and we've confirmed both sites work. The code is below. Is there something we're missing?
app.post('/site/app/getvalue', function(req, res){
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
console.log("Request: ");
console.log(req.body);
var options = {
method: 'post',
body: req.body,
json: true,
//url: 'https://X.X.X.X/getvalue/',
url : 'https://X.X.X.X:8443/getvalue/',
rejectUnauthorized: false,
headers: { 'Content-Type' : 'application/json'}
};
request(options,
function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log("Response: ");
console.log(body);
console.log("Transaction Count: " + body.TransactionCount);
res.send(body);
}
else {
console.log("ERROR: " + error);
res.send(500);
}
}
);
});
We get: ERROR: Error: socket hang up
Related
I am usdin the pentaho server to make calls to the web service in localhost:8080/pentaho/home.
The fact is that when I want to make a request to activate a transform(ktr) that I have stored in that location the call service return error 404.
The simple example not working: localhost:8080/kettle/status/?xml=Y .
I make the call from nodejs to api rest as it puts in the pentaho documentation
function launchTrans(req, res) {
let options = {
url: `http://localhost:8080/kettle/status/?xml=Y`,
method: 'GET',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Access-Control-Allow-Credentials': 'true',
'Cookie': globalString
},
};
request(options, (error, response, body) => {
if (!error && response.statusCode == 200) {
res.status(200).send(response);
} else {
console.log("There was an error " + error);
res.status(500).send(response);
}
});
}
The URL should be http://localhost:8080/pentaho/kettle/status/?xml=y not http://localhost:8080/kettle/status/?xml=y. Your URL is not quite properly formatted. The format you're using is for the old PDI standalone server. In the new merged server where reporting and PDI is merged into one server you need to include the "pentaho" part of the URL.
I have implemented a GET request via request module in Nodejs in an application. I need to display the response over html page.
However, the response returned is undefined. Also, error and body message is also undefined.
I tried running it seperately with "node file.js", it works fine. Do I have to add anything else while using it in application.
I also tried htttp, curl-request and request-promise modules, all of them return undefined.
var request = require(['request']);
var headers = {
'Accept': 'text/json',
'X-Auth-Token': tk,
'User-Agent': 'request'
};
var options = {
url: 'https://api.github.com/repos/request/request',
headers: headers,
method: 'GET',
json: true
};
function callback(error, response, body){
console.log(response);
console.log(error);
if (!error && response.statusCode == 200) {
console.log(body);
return body;
}
}
request(options, callback);
I just had the same issue and found out that the server certificate was not fully trusted. As a TEMPORARY solution you could try to add strictSSL: false to your options object.
If it works like this (and assuming that you did not really send the request to "https://api.github.com/repos/request/request") contact the service provider and tell them about the certificate issues.
I've been trying to hit an API and get some data back from it (It's a free API not my own). So I've got my API token and I've had a look around and found that npm package request seems to be the best.
Within one of my routes I have,
request({
uri: "https://app.url-to-api:443/api/list-of-data",
method: "GET",
api_token: "my-api-token",
timeout: 10000,
followRedirect: true,
maxRedirects: 10
}, function(error, response, body) {
console.log(body);
});
So I'm getting "message":"Authorization has been denied for this request." back which is obviously because my API Token isn't getting passed through.
This might be a stupid question, but where do I actually put the API token to validate my request?
Thanks!
In request it would be something like this:
request.get('http://some.server.com/', {
'auth': {
'bearer': 'bearerToken'
}
});
More details on what you can do with request are in the docs.
You have to pass api tokens in request headers please see the documentation for request
var request = require('request');
var options = {
url: 'https://api.github.com/repos/request/request',
headers: {
'Access-Token': 'request'
}
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
var info = JSON.parse(body);
console.log(info.stargazers_count + " Stars");
console.log(info.forks_count + " Forks");
}
}
request(options, callback);
We are trying to access a HTTPS REST API(GET) using nodeJS with request module, but it is throwing 401 - unauthorized.
We tried various options - provided like oauth_token OR user id & password, everything returns the same 401 status. I have given below the code snippet any suggestions would be helpful.
Note: We are running behind a proxy and we have set http proxy env variables, not sure if this will have anything to do with 401 (I don't think so but I am not 100 percent sure)
var request = require('request');
var options = {
uri: 'https://www.yammer.com/api/v1/messages.json',
auth: {
user: 'test',
pass: 'test-1',
sendImmediately: false
}
}
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log("Response is " + response);
} else {
console.log("Error is" + error);
console.log("Body is" + body);
console.log("Full response is" + JSON.stringify(response, null, 2));
}
});
Thanks.
I'm developing a node application which needs to authenticate with google. When I request a token, https://accounts.google.com/o/oauth2/token responds with:
error: 400
{
"error" : "invalid_request"
}
I've tried making the same request in curl, and have received the same error, so I suspect there is something wrong with my request but I can't figure out what. I've pasted my code below:
var request = require('request');
var token_request='code='+req['query']['code']+
'&client_id={client id}'+
'&client_secret={client secret}'+
'&redirect_uri=http%3A%2F%2Fmassiveboom.com:3000'+
'&grant_type=authorization_code';
request(
{ method: 'POST',
uri:'https://accounts.google.com/o/oauth2/token',
body: token_request
},
function (error, response, body) {
if(response.statusCode == 201){
console.log('document fetched');
console.log(body);
} else {
console.log('error: '+ response.statusCode);
console.log(body);
}
});
I've triple checked to make sure all the data I'm submitting is correct and i'm still getting the same error. What can I do to debug this further?
It turns out that request.js (https://github.com/mikeal/request) doesn't automatically include the content-length to the headers. I added it manually and it worked on the first try. I've pasted the code below:
exports.get_token = function(req,success,fail){
var token;
var request = require('request');
var credentials = require('../config/credentials');
var google_credentials=credentials.fetch('google');
var token_request='code='+req['query']['code']+
'&client_id='+google_credentials['client_id']+
'&client_secret='+google_credentials['client_secret']+
'&redirect_uri=http%3A%2F%2Fmyurl.com:3000%2Fauth'+
'&grant_type=authorization_code';
var request_length = token_request.length;
console.log("requesting: "+token_request);
request(
{ method: 'POST',
headers: {'Content-length': request_length, 'Content-type':'application/x-www-form-urlencoded'},
uri:'https://accounts.google.com/o/oauth2/token',
body: token_request
},
function (error, response, body) {
if(response.statusCode == 200){
console.log('document fetched');
token=body['access_token'];
store_token(body);
if(success){
success(token);
}
}
else {
console.log('error: '+ response.statusCode);
console.log(body)
if(fail){
fail();
}
}
}
);
}
from here How to make an HTTP POST request in node.js? you could use querystring.stringify to escape query string of request parameters. Plus you'd better add 'Content-Type': 'application/x-www-form-urlencoded' for POST request.
post here the final string generated from token_request var.that may have something wrong. or may be authentication code is expired or not added correctly to the URL. Usually code has '/' in it that needs to escaped.