make a post request with in node.js application - node.js

I've a node.js service with /api/authenticate endpoint. I can call this service successfully from POSTMAN with 'username' and 'password' as input (body parameters). How do I call the same services from another node.js server?
With postman I get,
body: {name: 'xxxxxx', password: 'xxxxxx' }
headers: { 'content-type': 'application/x-www-form-urlencoded',
host: 'xx.xx.xx.xx:xxxx',
connection: 'close',
'content-length': '0' }
POST /api/authenticate 200 1.336 ms - 72
Following is another nodejs application ... which makes a successful request call but doesn't have any body parameters (username and password) when it reaches to the authentication server api.
var my_http = require('http');
app.get('/makeacall', function(req, res) {
var output = '';
var options = {
body: { name: 'xxxxxx', password: 'xxxxxx' },
method: 'POST',
host: 'xx.xx.xx.xx',
port: 'xxxx',
path: '/api/authenticate',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
};
console.log('before request');
var req = my_http.request(options, function(response) {
console.log('response is: ' + response);
console.log('Response status code: ' + response.statusCode);
response.on('data', function(chunk) {
console.log('Data ..');
output += chunk;
});
response.on('end', function(chunk) {
console.log('Whole Data ..' + output);
});
});
req.on('error', function(err) {
console.log('Error: ' + err);
});
req.end();
console.log('444');
res.send({ message: 'View record message'});
});
From this nodejs application I get empty body on the server.
body: {}
headers: { 'content-type': 'application/x-www-form-urlencoded',
host: 'xx.xx.xx.xx:xxxx',
connection: 'close',
'content-length': '0' }
POST /api/authenticate 200 1.336 ms - 72
What am I missing? Any help is appreciated.

Using stock http library of NodeJS doesn't allow you to use that syntax.
Take a look at RequestJS as a much simpler solution. It will make your life a lot easier and allow you to use the syntax you want.
This is the solution to do it with stock Node.
https://nodejs.org/api/http.html#http_http_request_options_callback
Relevant Parts:
var postData = querystring.stringify({
'msg' : 'Hello World!'
});
And then, at the end:
// write data to request body
req.write(postData);
req.end();
But use a library unless you absolutely can't.

Are you trying to get the posted data from a form/etc?
Try using express.
npm install express -save
You can get posted data from a url with the ff:
app.post('*', function(request, response){
var post = {};
if(Object.keys(request.body).length){
for(var key in request.body){
post[key] = request.body[key];
console.log(key+'=>'+post[key];
}
}
});

Related

nodejs request post large json fail

I am trying to post large json to a http server(a grafana server actually):
here is my code:
const http = require('http')
const request = require('request')
const fs = require('fs')
const opts = {
hostname: 'myip',
port: 3000,
path: '/api/dashboards/uid/KPEiIQVWk',
method: 'GET',
timeout: 5000,
headers : {
'Authorization' : 'Bearer ********************************************',
'Accept' : 'application/json',
'Content-Type' : 'application/json'
}
}
const req = http.request(opts, res => {
console.log(`Fetch: statusCode: ${res.statusCode}`)
var origin = ''
res.on('data', d => {
origin += d
})
res.on('end', function(){
dash = JSON.parse(origin)
dash.dashboard.panels.forEach(p => {
if(p.id == 26){
fs.readFile(__dirname + '/grafana/pm/branch-graph.html','utf-8', function(err, newPanel){
if(err){
console.log(err)
}
p.content = newPanel
const fresh = JSON.stringify(dash)
const updateOptions = {
uri: 'http://myip:3000/api/dashboards/db',
method: 'post',
headers : {
'Authorization' : 'Bearer *************************',
'Accept' : 'application/json',
'Content-Type' : 'application/json',
'Content-length' : fresh.length
},
json: fresh
}
fs.writeFile('tmp.json', fresh, function(err){
if(err){
console.error(err)
}
})
request(updateOptions, function(error, response, body){
console.log(`update: statusCode=${response.statusCode}`)
console.log(`update: ${body}`)
})
})
}
})
})
})
req.on('error', error => {
console.error(error)
})
req.on('timeout', () => {
req.abort()
})
req.end()
as you can see, I first fetch a grafana dashboard's source, then make some udpate, then post it back to grafana server. but always get 400 error. The strange thing is that if I dump the json to a file and use curl to post, it will work.
curl -vH "Authorization: Bearer $TOKEN" -H "Expect:" -d #tmp.json -H "Content-Type:application/json" http://myip:3000/api/dashboards/db
the whole json is about 40000+ bytes. any hint on this? I am not very famillar with nodejs. I am just trying to write some CI scripts.
First, I don't think it's necessary to use both the http and request modules. http is a module built into nodejs, and request is an npm package.
I recommend you use the npm request package because it's easier. You can read its documentation here: https://www.npmjs.com/package/request#http-authentication
Second, the options you're passing to the request module is not formatted correctly, I think this is why it is not working. With your current code, I would console.log('POST error', error); to print out the error. The correct options for the request module is proposed below.
const options = {
url: 'https://myip:3000/api/dashboards/db',
body: fresh, // the json from the fs.read callback
auth: {
'bearer': 'bearerToken'
},
json: true // from docs: If json is true, then body must be a JSON-serializable object.
}
request.post(
options,
(err, httpResponse, body) => {
console.log(err, body);
});

Use native Node.JS code to get a WSO2 access_token

I have been trying to use native Node.JS code in Node 8 to get an access token from WSO2 with my client ID and client secret.
I receive the following error: Unsupported Client Authentication Method!
Here is my code:
const querystring = require('querystring');
const https = require('https');
var postData = querystring.stringify({
'grant_type' : 'client_credentials'
});
var options = {
hostname: 'api.somedomain.com',
port: 443,
path: '/token',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': postData.length
}
};
var req = https.request(options, (res) => {
console.log('statusCode:', res.statusCode);
console.log('headers:', res.headers);
res.on('data', (data) => {
process.stdout.write(data);
});
});
req.on('error', (err) => {
console.error(err);
});
req.write(postData);
req.end();
When I attempt to include another option parameter of 'auth' for the client ID and client secret then it tells me "TypeError: First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object."
Any help on how to make this work is greatly appreciated.
Looks like you are missing the Authorization header with the request. I'm no expert on javascript/node but the token generation works after adding the Authorization header in the headers section as below. I have used localhost for testing purpose.
var auth = 'Basic ' + Buffer.from("nM_ftrK2pjoBW4JofE21xI1cP0Ya" + ':' + "jmFJIgC5QMDkU_HxQKiDUbp5UAca").toString('base64');
var options = {
hostname: 'localhost',
port: 8243,
path: '/token',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': postData.length,
'Authorization': auth
}
};
The correct value (Authorization : Basic Base64(consumer-key:consumer-secret)) should be passed in with the token request when invoking the token endpoint to get the access_token.

How to authenticate using appid and appsecret in nodejs using https request

Here is the sample code for my application. Why It's not working ?
Error:
"{"error":"unauthorized","error_description":"An Authentication object was not found in the SecurityContext"}"
var post_data = JSON.stringify({
'appid' : results.appid,
'appsecret': results.appsecret,
});
var url = serviceUrl+"/oauth-service/oauth/token?grant_type=client_credentials";
var post_options = {
host: url,
headers: {
'Content-Type': 'application/json'
},
data:post_data
};
const req = https.request(post_options, function(res) {
res.setEncoding('utf8');
res.on('data',function(chunk){
console.log(chunk);
});
});
req.write(post_data);
req.end();
From the HTTPS request side, you don't need to have data in post_options (the data is sent using write).
More importantly, you need to set the Content-Length header to Buffer.byteLength(post_data).
Also, the host (or hostname) field should contain just the hostname (e.g. www.facebook.com - no https:// is needed) and the rest of the URL (e.g. /oauth_service/...) should be put in the path field.
Here's an example adapted from the node.js docs. They only provide examples for http but it's mostly the same as for https:
const postData = JSON.stringify({
'msg': 'Hello World!'
});
const options = {
hostname: 'www.google.com',
path: '/upload',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(postData)
}
};
const req = https.request(options, (res) => {
console.log(`STATUS: ${res.statusCode}`);
console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
res.setEncoding('utf8');
res.on('data', (chunk) => {
console.log(`BODY: ${chunk}`);
});
res.on('end', () => {
console.log('No more data in response.');
});
});
req.write(postData);
req.end();
Note that without specifying which service you're working with, it's impossible to say whether you're implementing the protocol correctly (e.g. whether the URL is correct, whether your request body is correct, etc.). So if you fix the Content-Length and things still don't work, please provide more information.

Updating post http request length in node.js

I'm using node.js to post a http request. the code works with if i define my post data ahead of the 'options' field, but if I initially set my post_data string to empty and update it later it doesn't pick up the new length. How would I get it to do that ? I'm looking to send multiple posts of varying lengths to the same place in a loop so need to be able to do this.
var post_data=''; //if i set my string content here rather than later on it works
var options = {
host: '127.0.0.1',
port: 8529,
path: '/_api/cursor',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': post_data.length
}
};
var req = http.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
post_data = 'a variable length string goes here';//the change in length to post_data is not //recognised
req.write(post_data);
req.end();
'Content-Length': post_data.length
You ran this before setting post_data.
If you want to set post_data after creating the object, you'll need to set it manually later:
options.headers['Content-Length'] = post_data.length;
Note that you must set that before calling http.request().
Posting data is a matter of sending a query string (just like the way you would send it with an URL after the ?) as the request body.
This also requires to declare Content-Type and Content-Length values so the server knows how to interpret the data.
var querystring = require('querystring');
var data = querystring.stringify({
username: yourUsernameValue,
password: yourPasswordValue
});
var options = {
host: 'my.url',
port: 80,
path: '/login',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': data.length
}
};
var req = http.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log("body: " + chunk);
});
});
req.write(data);
req.end();
You need to replace:
'Content-Length': post_data.length
for:
'Content-Length': Buffer.byteLength(post_data, 'utf-8')
See https://github.com/strongloop/express/issues/1870

Testing express app with mocha

I have the following request that I need to test:
curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"content":{"value":"18.5", "date": "20120413"}}' 'http://SERVER:PORT/marks'
I'm using expressjs and mocha. I did not find the way to add some header and specify some json parameters in a mocha's request:
it('Checks creation of a new mark', function(done){
request.post('http://SERVER:PORT/marks', function(err, response, body){
// Some headers and parameters should be set in the request
response.statusCode.should.equal(201);
done();
});
});
The test below (GET request) works well though:
it('Checks existence of marks for user dummyuser', function(done){
request.get('http://SERVER:PORT/user/dummyuser/marks', function(err, response, body){
response.statusCode.should.equal(200);
done();
});
});
UPDATE
The following works like a charm: (I though request what some kind of variable created by mocha).
request(
{ method: 'POST'
, uri: 'http://SERVER:PORT/marks'
, headers: { 'content-type': 'application/json' , 'accept': 'application/json' }
, json: { "content":{"value":"18,5", "date": "2012-04-13"} }
}
, function(err, response, body){
response.statusCode.should.equal(201);
done();
});
Take a look at the documentation. There is a great explaination of how to do a post with custom headers. One way of doing it which works for me could be the following.
var options = {
host: 'localhost',
port: 80,
path: '/echo/200',
method: 'POST',
headers: {
"X-Terminal-Id" : terminalId
}
};
var data = ""
var req = https.request(options, function(res) {
res.on('data', function(d) {
data += d;
});
res.on('end', function(err){
//Check that data is as expected
done(null, data)
})
});
req.end();
req.on('error', function(err) {}
done(err)
});

Resources