Hello. I’m trying to send an object to my server, but I can’t see how.
I’ve already tried:
Client:
module.exports = async function(params, keys) {
return new Promise((resolve, reject) => {
// select http or https module, depending on reqested url
let url
if (params.ops.target) {url = `${keys.domain}push?params=${params.id}&value=${JSON.stringify(params.data)}&target=${params.ops.target}&password=${keys.password}`} else url = `${keys.domain}push?params=${params.id}&value=${JSON.stringify(params.data)}&password=${keys.password}`
const lib = url.startsWith('https') ? require('https') : require('http');
const request = lib.get(url, (response) => {
// handle http errors
if (response.statusCode < 200 || response.statusCode > 299) {
reject(new Error('Failed to make request, status code: ' + response.statusCode));
}
// temporary data holder
const body = [];
// on every content chunk, push it to the data array
response.on('data', (chunk) => body.push(chunk));
// we are done, resolve promise with those joined chunks
response.on('end', () => resolve(body.join('')));
});
// handle connection errors of the request
request.on('error', (err) => reject(err))
})
Usage: https://example.com/push?params=test&value={my:%27obj%27}&password=1234
Server:
app.get("/push", checkpass, async (req, res) => {
let params = req.query.params,
value = req.query.value,
target = req.query.target
if (params) {
if (value) {
if (target) {
try {
await db.push(params, JSON.parse(value), {target: `.${target}`})
res.status(200).json({message: `${JSON.parse(value)} pushed to ${params}.${target}`})
} catch(e) {
res.status(200).send(e)
}
} else {
try {
await db.push(params, JSON.parse(value))
res.status(200).json({message: `${JSON.parse(value)} pushed to ${params}`})
} catch(e) {
return res.status(200).send(e)
}
}
} else return res.status(200).json({message: "No value to insert.", code: 401})
} else return res.status(200).json({message: "No database entry specified.", code: 401})
});
Anyone knows any way to send objects in that url path with node.js native packages?
EDIT: I tried this code:
module.exports = async function(params, keys) {
return new Promise((resolve, reject) => {
let url
let domain = ''
const myObj = params.data
if (params.ops.target) {url = `/push?params=${params.id}&target=${params.ops.target}&password=${keys.password}`} else url = `/push?params=${params.id}&password=${keys.password}`
/*if (keys.domain.includes('https://')) {
domain = keys.domain.replace('https://', '')
} else {
if (keys.domain.includes('http://')) {
domain = keys.domain.replace('http://', '')
}
}*/
var options = {
hostname: domain,
path: url,
method: 'POST',
//body: myobj,
headers: {
'Content-Type': 'application/json',
'Content-Length': myObj.length
}
};
const lib = keys.domain.startsWith('https') ? require('https') : require('http');
const request = lib.request(options, (response) => {
// handle http errors
if (response.statusCode < 200 || response.statusCode > 299) {
reject(new Error('Failed to make request, status code: ' + response.statusCode));
}
// temporary data holder
const body = [];
// on every content chunk, push it to the data array
response.on('data', (chunk) => body.push(chunk));
// we are done, resolve promise with those joined chunks
response.on('end', () => resolve(body.join('')));
});
// handle connection errors of the request
request.on('error', (err) => reject(err))
})
}
But it gave me this: