I am new to most of these concepts, so I apologize if this question is trivial. I am trying to write a script that makes an HTTP POST request that sends a .json file containing an array of jsons. I am using an npm module found here: https://github.com/request/request and a tutorial that walks you through using the module found here: http://samwize.com/2013/08/31/simple-http-get-slash-post-request-in-node-dot-js/
Here is the code I have so far:
//var fs = require('fs');
var request = require('request');
// Set the headers
var headers = {
'Content-Type': "stuff",
'x-authorization': "stuff"
}
// Configure the request
var options = {
url: 'http://localhost:8080/users/add',
method: 'POST',
headers: headers,
form: {
'username': 'testuser42',
'firstName': 'test',
'lastName': 'user42',
'password': 'testpassword'
}
}
// Start the request
request(options, function(error, response, body){
if (!error && response.statusCode == 200) {
console.log(body)
}
})
The data.json file I am trying to send to the local server will contain an array of jsons, formatted like so:
[
{
"username": "testuser1",
"firstName": "test",
"lastName": "user1",
"password": "password1'
},
{
"username": "testuser2",
"firstName": "test",
"lastName": "user2",
"password": "password2'
}
]
So I think I need to make separate POST requests for each array element, but it is not clear to me how to do this.