Querystring of a javascript object - node.js

I need to make a HTTP GET request but i have some trouble with making a querystring of a javascript object. This is my object.
var params = {
from: {
zip: '42100',
country: 'IT'
},
to: {
zip: '20019',
country: 'IT'
},
packages: [ { "width": 50, "height": 40, "length": 40, "weight": 2 } ]
};
I manually made my querystring and this is the result
from[zip]=42100&from[country]=IT&to[zip]=20019&to[country]=IT&packages[0]=[width]=50&[height]=40&[length]=40&[weight]=2
The problem is that Google says "Your client has issued a malformed or illegal request."
This is my NodeJS script.
var request = require('request');
var params = {
from: {
zip: '42100',
country: 'IT'
},
to: {
zip: '20019',
country: 'IT'
},
packages: [ { "width": 50, "height": 40, "length": 40, "weight": 2 } ]
};
function packagesToQueryString(packages) {
let stringa = "";
for (const onePackage of packages) {
stringa += '[width]='+ onePackage.width + '&[height]='+ onePackage.height +'&[length]='+ onePackage.length +'&[weight]='+ onePackage.weight +'';
}
return 'packages[' + (packages.length - 1) + ']=' + stringa;
}
function paramsToQueryString(obj) {
return 'from[zip]=' + obj.from.zip +'&from[country]=' + obj.from.country + '&to[zip]=' + obj.to.zip + '&to[country]=' + obj.to.country+ '&';
}
const formData = paramsToQueryString(params) + packagesToQueryString(params.packages);
console.log(formData);
request({
headers: {
'Authorization': 'fcd3dda8...2577',
'Content-Type': 'application/json'
},
body: formData,
uri: 'https://api.packlink.com/v1/services',
method: 'GET'
}, function (err, res, body) {
console.log(body);
});
Google error

There are 2 reasons that causes the "malformed or illegal request" error:
In your HTTP request, the Content-Type is defined as application/json. However, the value of body is formData (from[zip]=42100&from[country]=...), which is NOT json.
Even for the formData, your assemble logic is incorrect. For params object, its corresponding query string is: from%5Bzip%5D=42100&from%5Bcountry%5D=IT&to%5Bzip%5D=20019&to%5Bcountry%5D=IT&packages%5B0%5D%5Bwidth%5D=50&packages%5B0%5D%5Bheight%5D=40&packages%5B0%5D%5Blength%5D=40&packages%5B0%5D%5Bweight%5D=2
In order to successfully send that HTTP GET request, you need to use the qs option of request module. The code would look like below:
var request = require('request');
var params = {
from: {
zip: '42100',
country: 'IT'
},
to: {
zip: '20019',
country: 'IT'
},
packages: [ { "width": 50, "height": 40, "length": 40, "weight": 2 } ]
};
request({
headers: {
'Authorization': 'fcd3dda8...2577'
},
qs: params,
uri: 'https://api.packlink.com/v1/services',
method: 'GET'
}, function (err, res, body) {
console.log(body);
});

Related

Pagination doesn't work with datatables and nodejs/elasticsearch implementation

I'm using nodejs as back/front-end implementation for a quick UI overview of an index in nodejs, and I'd like to use datatables with server side processing to display the data in a properly formated table.
It grabs all of the data fine, however it just dumps all of the records into one page on the table.
Model:
const { Client } = require('#elastic/elasticsearch')
const client = new Client({
node: '',
auth: {
username: '',
password: ''
}
})
module.exports = function getElasticData(callback/*, size*/) {
//parseInt(inputSize) = size;
client.search({
index: 'anomalies02_bhc-lstm2',
size: 10000,
body: {
query: {
match_all: {}
}
}
}, function (error, response, status) {
if (error) {
console.log("search error: " + error)
}
if (response) {
var elasticList = [];
//console.log("EL:" + JSON.stringify(response))
response.body.hits.hits.forEach(function (hit) {
elasticList.push(hit);
})
//callback(elasticList.sort());
//console.log("SORTED LIST: " + JSON.stringify(elasticList))
callback(elasticList);
}
else {
console.log("<p>No results</p>");
}
});
}
Controller:
var elasticDataModel = require('../model/getDataElastic');
exports.getData = function (req, res) {
elasticDataModel(function (elasticList) {
var searchStr = req.body.search.value;
var recordsTotal = 0;
var recordsFiltered = 0;
var size = parseInt(req.body.length);
var recordsFiltered = elasticList.slice(0, size)
console.log("LENGTH: " + JSON.stringify(req.body.length))
console.log("FILTERED: " + JSON.stringify(recordsFiltered))
//console.log(elasticList[0]._source);
console.log(typeof parseInt(req.body.draw))
console.log(elasticList.length)
recordsTotal = elasticList.length;
//console.log("DRAW " + req.body.draw);
var data = JSON.stringify({
"data": elasticList,
"draw": parseInt(req.body.draw),
"recordsTotal": recordsTotal,
"recordsFiltered": recordsFiltered
});
res.send(data);
});
}
HTML:
$(document).ready(function () {
var t = $('#example2').DataTable({
"paging": true,
"processing": true,
"serverSide": true,
'ajax': {
'type': 'POST',
'url': '/populateData'
},
'pageLength': 20,
'lengthMenu': [5, 10, 20, 50, 100, 200, 500],
'columns':
[
{ 'data': '_id', "defaultContent": "", 'name': 'ID' },
{ "defaultContent": "", 'name': 'Kibana Link' },
{ 'data': '_source.Environment', "defaultContent": "", 'name': 'Environment' },
{ 'data': '_source.Cause', "defaultContent": "", 'name': 'Downtime cause' },
{ 'data': '_source.Start', "defaultContent": "", 'name': 'Detected start' },
{ 'data': '_source.End', "defaultContent": "", 'name': 'Detected end' },
{ "defaultContent": "", 'name': 'Actual start' },
{ "defaultContent": "", 'name': 'Actual end' },
{ "defaultContent": "", 'name': 'Reason category' },
{ "defaultContent": "", 'name': 'Reason details' },
{ "defaultContent": "", 'name': 'Submit' },
],
"columnDefs": [
{
"searchable": true,
"orderable": true,
"targets": 0
}
]
});
});
I modifed my code accordingly with help from Kevin at the datatables forums and now it works flawlessly.
Changed code:
Controller
var elasticDataModel = require('../model/getFilteredDataElastic');
var pageLength;
var pageStart;
exports.getData = function (req, res) {
pageLength = req.body.length;
pageStart = req.body.start
console.log("MODEL Length: " + pageLength)
elasticDataModel(pageLength, pageStart, function (elasticFilteredList) {
var searchStr = req.body.search.value;
var recordsTotal = 0;
var recordsFiltered = 0;
console.log("PAGE LENGTH: " + pageLength)
if (req.body.search.value) {
var regex = new RegExp(req.body.search.value, "i")
searchStr = { $or: [{ '_id': regex }, { 'Environment': regex }, { 'Downtime cause': regex }, { 'Detected start': regex }, { 'Detected end': regex }, { 'Reason details': regex }] };
}
else {
searchStr = {};
}
var size = parseInt(req.body.length);
var recordsFiltered = elasticFilteredList
var data = JSON.stringify({
"data": elasticFilteredList,
"draw": parseInt(req.body.draw),
"recordsTotal": elasticFilteredList[0],
"recordsFiltered": recordsFiltered
});
res.send(data);
});
}
Model:
const { Client } = require('#elastic/elasticsearch')
const client = new Client({
node: '',
auth: {
username: '',
password: ''
}
})
module.exports = function getElasticData(arg, arg2, callback) {
console.log("CONTROLLER SIZE: " + arg)
console.log("CONTROLLER FROM: " + arg2)
client.search({
index: 'anomalies02_bhc-lstm2',
from: arg2,
size: arg,
body: {
query: {
match_all: {}
}
}
}, function (error, response, status) {
if (error) {
console.log("search error: " + error)
}
if (response) {
var elasticFilteredList = [];
console.log("VALUE: " + response.body.hits.total.value);
elasticFilteredList.push(response.body.hits.total.value);
response.body.hits.hits.forEach(function (hit) {
elasticFilteredList.push(hit);
})
callback(elasticFilteredList);
}
else {
console.log("<p>No results</p>");
}
});
}

Spotify: Create Playlist

Extending https://github.com/spotify/web-api-auth-examples - the code in the authorization_code folder.
It logs out access_token okay, but then hangs at the post request:
app.get('/createPlaylist', function(req, res) {
console.log('access_token=' + access_token)
request.post(
'https://api.spotify.com/v1/users/' + user_id + '/playlists',
{
headers: {
'Authorization' : access_token,
'Content-Type': 'application/json'
},
json: {
"collaborative": false,
"description": null,
// "external_urls": {
// "spotify": "http://open.spotify.com/user/thelinmichael/playlist/7d2D2S200NyUE5KYs80PwO"
// },
// "followers": {
// "href": null,
// "total": 0
// },
// "href": "https://api.spotify.com/v1/users/thelinmichael/playlists/7d2D2S200NyUE5KYs80PwO",
// "id": "7d2D2S200NyUE5KYs80PwO",
"href": null,
"id": null,
"images": [],
"name": "A Generated Playlist",
"owner": {
"external_urls": {
"spotify": "http://open.spotify.com/user/1150816110"
},
"href": "https://api.spotify.com/v1/users/1150816110",
"id": "1150816110",
"type": "user",
"uri": "spotify:user:1150816110"
},
"public": true,
// "snapshot_id": "s0o3TSuYnRLl2jch+oA4OEbKwq/fNxhGBkSPnvhZdmWjNV0q3uCAWuGIhEx8SHIx",
"tracks": {
"href": "https://api.spotify.com/v1/users/thelinmichael/playlists/7d2D2S200NyUE5KYs80PwO/tracks",
// "href": "https://api.spotify.com/v1/users/kb8mc65qdvz4lz0gdk0i4ztp3/playlist/46RFjEgbskxglR8rVsu38x/tracks",
"items": [],
"limit": 100,
"next": null,
"offset": 0,
"previous": null,
"total": 0
},
"type": "playlist",
// "uri": "spotify:user:thelinmichael:playlist:7d2D2S200NyUE5KYs80PwO"
}
},
function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
}
);
});
Any idea what is wrong?
'It looks like your post is mostly code; please add some more details.'
What details StackOverflow? I just wanna know why the POST request hangs.
Modified my code to Kevin's specs ... can't believe I misread output for input ... but it still hangs ... node version v12.14.1
app.get('/createPlaylist', function (req, res) {
console.log('access_token=' + access_token)
request.post(
'https://api.spotify.com/v1/users/' + user_id + '/playlists',
{
headers: {
'Authorization': access_token,
'Content-Type': 'application/json'
},
json: {
name: 'A generated playlist',
public: true,
collaborative: false,
description: 'Generated at ' + Date.now(),
}
},
function (error, response, body) {
if (!error && response.statusCode === 200) {
console.log(body);
}
}
);
});
Comparing your code to the documentation, it looks like the code is sending the JSON output as the input in the example.
To create a playlist, there are only 4 body parameters:
name - string - required
public - boolean - optional
collaborative - boolean - optional
description - string - optional.
I don't know where all the other information is coming from but the API could be hanging up on all of it.
Also be sure you have the right scopes involved with permission to create a playlist for a user.
The request would look trimmed down:
request.post(
'https://api.spotify.com/v1/users/' + user_id + '/playlists',
{
headers: {
'Authorization' : access_token,
'Content-Type': 'application/json'
},
json: {
name: 'A generated playlist',
public: true,
description: 'A playlist generated by JS'
}
}
...

using request-promise to querystring in JSON

I have a big API I want to query for userId and receive its details.
var options = {
uri: 'http://www.theapi.net/0862710324bo0',
method : 'GET',
useQuerystring: true,
qs: {
"must": [
{ "match": { "data.clients.id": req.params.userId }},
]
},
headers: {
'User-Agent': 'Request-Promise'
},
json: true // Automatically parses the JSON string in the response
};
console.log(options.qs.must)
rp(options)
.then(function (repos) {
console.log(repos.clients.name);
res.status(200).json({
data:repos.clients[0].name
})
})...
This code returns:
[
{
match: { 'data.clients.id': 'b2d445-2160-4va7-ref-4edf860bd' }
}
]
undefined (because I didn't specify the object array index)
{
"data": "Sergio"
}
What I need:
{
"id":"ec9c1c4d-ab1a-41b2-bc1a-520b889cdeb9",
"name":"Sergio",
"email":"sergio#jorge.com",
},
I believe adding a "bool" tag would help you out.
var options = {
uri: 'http://www.theapi.net/0862710324bo0',
method : 'GET',
useQuerystring: true,
qs: {
"bool": { // Tag added
"must": [
{ "match": { "data.clients.id": req.params.userId }},
]
}
}
headers: {
'User-Agent': 'Request-Promise'
},
json: true // Automatically parses the JSON string in the response
};
console.log(options.qs.must)
rp(options)
.then(function (repos) {
console.log(repos.clients.name);
res.status(200).json({
data:repos.clients[0].name
})
})
Beware - Untested code!
for (const [key, value] of Object.entries(repos.clients)) {
if (req.params.userId === repos.clients[key].id) {
return res.status(200).json({
data:repos.clients[key]
})
}
}

Provisional headers are shown

I am using Extjs in web and node.js in the api side. I called the api defined, from the web. I passed params and required data as request, but its showing the below error and request is unsuccessful.
Here is the api call code:
Ext.Ajax.request({
url: "http://localhost:3000/update-fund",
method: "POST",
dataType: 'json',
params: {
userId: userDocGlobal.id
},
headers: {
'Content-Type': 'application/json'
},
jsonData: {
fundId: PECalculator.selectedFund,
fundDate: getAiqDateFromPicker(Ext.getCmp("currentCalculationDateFundLevel")),
fundData: fund,
ownerId: userDocGlobal.companyid,
lpId: userDocGlobal.companyid,
role: userDocGlobal.role,
userId: userDocGlobal.id,
companyid: userDocGlobal.companyid
},
success: function (response) {
if (callback) {
callback(response);
}
if(userRole === "FREE"){
moduleObj.setFundNameAndCalcDateForFreeUser();
}
},
error: function () {
if (errorCallback) {
errorCallback();
}
}
})

Unable to create Eventbrite event using API

I am trying to create an event using V3 API. The parameters I am sending are:
var _event = {
"event" : {
"name": {
"html": "Test Event 01"
},
"description": {
"html": "Test Event 01"
},
"start": {
"timezone": "America/Chicago",
"utc": "2017-07-10T18:00:00Z"
},
"end": {
"timezone": "America/Chicago",
"utc": "2017-07-10T20:00:00Z"
},
"currency": "USD"
}
};
This is how I am sending the request using NodeJS:
var request = require('request');
var _headers = {
'Authorization': 'Bearer ' + _token,
'Content-Type': 'application/json',
}
// Configure the request
var options = {
url: EVENTBRITE_API_URL + "events/",
method: 'POST',
headers: _headers,
form: _event
}
I have also tried Content-Type: application/x-url-form-encoded and inside options, JSON.stringify(_event). I also tried to change _event object to:
var _event = {
"name": {
"html": "Test Event 01"
},
"description": {
"html": "Test Event 01"
},
"start": {
"timezone": "America/Chicago",
"utc": "2017-07-10T18:00:00Z"
},
"end": {
"timezone": "America/Chicago",
"utc": "2017-07-10T20:00:00Z"
},
"currency": "USD"
};
I have tried all the combinations but I always get the same response:
{
"status_code": 400,
"error_description": "There are errors with your arguments: event[currency] - Unknown parameter, event.start.timezone - This field is required., event.currency - This field is required., event.start.utc - This field is required., event[start][timezone] - Unknown parameter, event.end.utc - This field is required., event[end][utc] - Unknown parameter, event[description][html] - Unknown parameter, event[name][html] - Unknown parameter, event[end][timezone] - Unknown parameter, event[start][utc] - Unknown parameter, event.end.timezone - This field is required., event.name.html - This field is required.",
"error": "ARGUMENTS_ERROR"
}
Is there any sample create event request anywhere I can look into? What is the problem with above request?
I think the issue is that you're currently sending a form post (by virtue of using the form option), but then your parameters should look like { "event.currency": "USD", "event.name.html": "Test Event 01", ... }. If you want to send JSON, you need to use json: true and pass a JSON-serializable body:
var request = require('request');
var _headers = {
'Authorization': 'Bearer ' + _token,
'Content-Type': 'application/json',
}
// Configure the request
var options = {
url: EVENTBRITE_API_URL + "events/",
method: 'POST',
headers: _headers,
// Instead of this:
// form: _event
// use this:
json: true, // says you're sending JSON
body: _event, // so the body should be JSON-serializable
};
If you want to stick with a form post, I think this should do it:
var _event = {
"event.name.html": "Test Event 01",
"event.description.html": "Test Event 01",
"event.start.timezone": "America/Chicago",
"event.start.utc": "2017-07-10T18:00:00Z",
"event.end.timezone": "America/Chicago",
"event.end.utc": "2017-07-10T20:00:00Z",
"event.currency": "USD",
};
var request = require('request');
var _headers = {
'Authorization': 'Bearer ' + _token,
'Content-Type': 'application/x-www-form-urlencoded',
}
// Configure the request
var options = {
url: EVENTBRITE_API_URL + "events/",
method: 'POST',
headers: _headers,
form: _event,
};

Resources