Nodejs - Rest api call with option --data from a file - node.js

I am making rest api calls from a nodejs application.
My curl calls looks like this:
curl -X PUT -iv -H "Authorization: bearer <token>" -H "Content-Type: application/json" -H "Accept: application/json" -H "X-Spark-Service-Instance: <spark-instance>" --data "#pipeline.json" -k https://<url>
I want to have a similar call in Nodejs. I am unable to understand how to have the data sent which is in a json file which in curl call was --data "#pipeline.json".
My Nodejs code looks like this:
var token = req.body.mlToken;
var urlToHit = req.body.url;
var SPARKINSTANCE = req.body.sparkInstance;
var b = "bearer ";
var auth = b.concat(token);
var headers = {
'Content-Type': 'application/json',
'Authorization': auth,
'Accept': 'application/json',
'X-Spark-Service-Instance': SPARKINSTANCE
}
var options= {
url: urlToHit,
method: 'PUT',
headers: headers
}
console.log(urlToHit);
request(options, callback);
function callback(error, response, body) {...}

You can either use the request library to pipe the request as so:
var fs = require('fs');
var options= {
url: urlToHit,
method: 'PUT',
headers: headers
}
fs.createReadStream('./pipeline.json')
.pipe(request.put(options, callback))
Or, using plain Node.js, read the file in to memory asynchronously and the once loaded, make a put request like this:
var fs = require('fs');
// Will need this for determining 'Content-Length' later
var Buffer = require('buffer').Buffer
var headers = {
'Content-Type': 'application/json',
'Authorization': auth,
'Accept': 'application/json',
'X-Spark-Service-Instance': SPARKINSTANCE
}
var options= {
host: urlToHit,
method: 'PUT',
headers: headers
}
// After readFile has read the whole file into memory, send request
fs.readFile('./pipeline.json', (err, data) => {
if (err) throw err;
sendRequest(options, data, callback);
});
function sendRequest (options, postData, callback) {
var req = http.request(options, callback);
// Set content length (RFC 2616 4.3)
options.headers['Content-Length'] = Buffer.byteLength(postData)
// Or other way to handle error
req.on('error', (e) => {
console.log(`problem with request: ${e.message}`);
});
// write data to request body
req.write(postData);
req.end();
}

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);
});

POST using multipart/form-data in NodeJS

I need to essentially POST a local image to Cisco Webex room from my NodeJS service. For local files, you need to do a multipart/form-data request instead of JSON as mentioned in the documentation.
The CURL looks like
curl --request POST \
--header "Authorization: Bearer ACCESS_TOKEN" \
--form "files=#/home/desktop/example.png;type=image/png" \
--form "roomId=Y2lzY2....." \
--form "text=example attached" \
https://api.ciscospark.com/v1/messages
But I am not sure how to convert it to nodeJS request format. I tried to use CURL to Node request converter here but doesn't seem like it is handling the multipart/form-data type. Please suggest.
EDIT: after doing some research, I came up with the below code
var request = require('request');
var fs = require('fs');
var params = { roomId: ROOMID,
text: "hello....",
files: {
value: fs.createReadStream(PATH_WO_FILENAME),
options: {
filename: 'image.jpg',
contentType: 'jpg'
}
}
};
var headersWebex = {
'Authorization': 'Bearer MY_BOT_ACCESS_TOKEN',
'Content-Type': 'multipart/form-data' }
request.post({
headers: headersWebex,
url: 'https://api.ciscospark.com/v1/messages',
method: 'POST',
body: params
}, function(error, response, body){
console.log(body);
});
But it is throwing error
undefined
_http_outgoing.js:642
throw new TypeError('First argument must be a string or Buffer');
Ok so here is how I made it work. I essentially needed to look deeper into the docs that #Evan mentioned
var request = require('request');
var fs = require('fs');
var roomID = 'MY_ROOM_ID'
var params = {
roomId: roomID,
text: "hello....",
files: {
value: fs.createReadStream('./image.jpg'),
options: {
filename: 'image.jpg',
contentType: 'image/jpg'
}
}
};
var headersWebex = {
'Authorization': 'Bearer MY_BOT_ACCESS_TOKEN',
'Content-Type': 'application/json'
}
request.post({
headers: headersWebex,
url: 'https://api.ciscospark.com/v1/messages',
method: 'POST',
formData: params
}, function(error, response, body){
if (error)
console.log(error)
console.log(body);
});

How to use bearer token for authorization for POST method in sync-request?

How can we use bearer token with POST method using npm sync-request? The sync-request resource page has the way to use authorization in GET request but not in POST request.
*******GET Request*******
var request = require('sync-request');
var res = request('GET', 'https://example.com', {
'headers': {
'user-agent': 'example-user-agent'
}
});
****POST Request*****
var request = require('sync-request');
var res = request('POST', 'https://example.com/create-user', {
json: { username: 'Name' }
});
Not sure why you would want to use sync-request which can cause timing issues but this should work with either sync-request or request
// *******GET Request*******
var request = require('sync-request');
var res = request('GET', 'https://example.com', {
'headers': {
'user-agent': 'example-user-agent',
'authorization', 'Bearer ' + authId
}
});
// ****POST Request*****
var request = require('sync-request');
var res = request('POST', 'https://example.com/create-user', {
'headers': {
'authorization', 'Bearer ' + authId
},
json: { username: 'Name' }
});
authId needs to be whatever your bearer token spoils be for your app.
I would suggest use of axis and example below:-
GET
import axios from "axios";
axios({
method: 'get',
url: url,
headers: {
'Content-Type': 'application/json'
}
}).then(function (response) {
console.log(response);
}).catch((err) => {
console.log(err)
));
POST
axios({
method: 'post',
url: url,
data: JSON.stringify({orders}),
headers: {
'Content-Type': 'application/json',
'Authorization': userObj.token
}
}).then(function (response) {
console.log(response)
});
Where ubserObj.token -
Bearer Token ex: Bearer ASDF#!##!ADFASDF!##!##
This will be on the server side settings.

Simple NodeJS http request equivalent for curl

I'm having trouble converting a curl that works to an equivalent http request through nodeJS. I'm using the Request module, but I seem to be doing something wrong when making the request. When I run it, it gives me
body: Cannot POST /path
Not really sure how to debug this, any ideas?
var data = JSON.stringify({
'sender': {
'name': 'name',
'handle': 'handle'
},
'subject': 'Title here',
'body': 'something something',
'metadata': {}
});
var options = {
host: 'website.com',
path: '/path',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer <token>',
'Accept': 'application/json',
'Content-Length': Buffer.byteLength(data)
}
};
var req = http.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log("body: " + chunk);
});
});
req.write(data);
req.end();
Below is the equivalent curl (that works) that I'm trying to make for the above nodejs.
curl --include \
--request POST \
--header "Content-Type: application/json" \
--header "Authorization: Bearer <token>" \
--header "Accept: application/json" \
--data-binary "{
\"sender\": {
\"name\": \"name\",
\"handle\": \"handle\"
},
\"subject\": \"Title here\",
\"body\": \"something something\",
\"metadata\": {}
}" \
'website.com/path"
You can include your JSON data directly with json parameter with request library :
var request = require('request');
var options = {
uri: 'http://website.com/path',
method: 'POST',
headers: {
'Authorization': 'Bearer <token>',
'Accept': 'application/json'
},
json: {
'sender': {
'name': 'name',
'handle': 'handle'
},
'subject': 'Title here',
'body': 'something something',
'metadata': {}
}
};
var req = request(options, function(error, response, body) {
if (error) {
console.log(error);
return;
}
if (response.statusCode == 200) {
console.log(body);
} else {
console.log("receive status code : " + response.statusCode);
}
});
From request options doc :
json - sets body to JSON representation of value and adds
Content-type: application/json header. Additionally, parses the
response body as JSON.

cURL call to API in NodeJS Request

it's me again with another lame question. I have the following call to a Rattic password database API which works properly:
curl -s -H 'Authorization: ApiKey myUser:verySecretAPIKey' -H 'Accept: text/json' https://example.com/passdb/api/v1/cred/\?format\=json
I tried to replicate this call in NodeJS, however the following returns blank:
var request = require('request');
url='https://example.com/passdb/api/v1/cred/?format=json';
request({
url: url,
method: 'POST',
headers: [
{ 'Authorization': 'ApiKey myUser:verySecretAPIKey' }
],
},
function (error, response, body) {
if (error) throw error;
console.log(body);
}
);
Any help is appreciated.
As pointed out in the comments already, use GET, not POST;
headers should be an object, not an array;
You're not adding the Accept header.
All combined, try this:
request({
url : url,
method : 'GET',
headers : {
Authorization : 'ApiKey myUser:verySecretAPIKey',
Accept : 'text/json'
}, function (error, response, body) {
if (error) throw error;
console.log(body);
}
});
One thing you can do is import a curl request into Postman and then export it into different forms. for example, nodejs:
var http = require("https");
var options = {
"method": "GET",
"hostname": "example.com",
"port": null,
"path": "/passdb/api/v1/cred/%5C?format%5C=json",
"headers": {
"authorization": "ApiKey myUser:verySecretAPIKey",
"accept": "text/json",
"cache-control": "no-cache",
"postman-token": "c3c32eb5-ac9e-a847-aa23-91b2cbe771c9"
}
};
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();
Headers should be an object.
var request = require('request');
url='https://example.com/passdb/api/v1/cred/?format=json';
request({
url: url,
method: 'POST',
headers: {
'Authorization': 'ApiKey myUser:verySecretAPIKey'
}
}, function (error, response, body) {
if (error) throw error;
console.log(body);
});

Resources