My current challenge involves attempting to send an array of JSON objects to a local server using an HTTP 'POST' request. I've been utilizing an npm module from here to handle the request. While my code has generally worked fine in the past, I am now facing an unresolved issue.
var request = require('request');
const obj = require('./bugs.json');
obj.forEach(element => {
// Set the headers
var headers = {
'Content-Type': "stuff",
'x-authorization': "stuff"
}
// Configure the request
var options = {
url: 'http://localhost:8080/bugreport/import',
method: 'POST',
headers: headers,
form: {
'subject': element.subject,
'description': element.description,
'platform': element.platform,
'date': element.date,
'author': element.author,
'isSolved': element.isSolved,
'createdAt': element.createdAt,
'updatedAt': element.updatedAt,
'id': element.id
}
}
//
// Start the request
request(options, function(error, response, body){
if (!error && response.statusCode == 200) {
console.log("No error detected.");
console.log(body)
}
else {
console.log("Error: " + error);
}
})
});
The specific line causing issues is 'author': element.author,
. All other attributes are basic data types, but author itself is a nested JSON. The structure of the JSON file is as follows:
[
{
"subject": "Bug",
"description": "Testing brscript",
"platform": "A",
"date": "2/15/2018",
"author": [
{
"username": "testuser1",
"firstName": "test",
"lastName": "user1",
"password": "password",
"admin": false,
"isVisible": true,
"isDeleted": false,
"createdAt": "2018-02-08T18:42:58.473Z",
"updatedAt": "2018-02-13T15:51:17.954Z",
"id": 170
}
],
"isSolved": false,
"createdAt": "2/15/2018",
"updatedAt": "2/15/2018",
"id": 999
}
]
I have prior experience with similar tasks, but not with a nested JSON as part of the data. Every attribute except for the author JSON successfully sends. There seems to be an issue with how I'm handling the syntax related to the nested JSON. Various attempts to resolve this have proven unsuccessful.