3

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.

5
  • 1
    why do you think you need to do two separate requests? Commented Jan 30, 2018 at 15:42
  • I thought the body of my POST request could only contain one json object. Commented Jan 30, 2018 at 15:47
  • to answer your question, that entire data.json is a JSON file, therefore the entirety can be sent. the only thing you should ask yourself tis the potential size Commented Jan 30, 2018 at 15:48
  • The size of the array will be variable, it won't always contain just two objects. Commented Jan 30, 2018 at 15:51
  • check the boring answer i did, Commented Jan 30, 2018 at 15:53

1 Answer 1

3

Here is trivial example. The body needs to be a JSON type, doesnt matter the number of items, as long as the JSON is formatted properly your are good to go!

  const obj= {'msg': [
  {
    "username": "testuser1",
    "firstName": "test",
    "lastName": "user1",
    "password": "password1"
  },
  {
    "username": "testuser2",
    "firstName": "test",
    "lastName": "user2",
    "password": "password2"
  }
]};

request.post({
    url: 'your website.com',
    body: obj,
    json: true
  }, function(error, response, body){
  console.log(body);
});

To include json file, just use require function like normal.

1: const obj = require('./path_to/data.json');

Sign up to request clarification or add additional context in comments.

6 Comments

you can use promises here, instead of callbacks, just wanted to use the simplest example
another great alternative is Axios,
But in my case the 'obj' containing json items is in a separate, local file. So I guess what I'm asking is how to read that file into my script?
Thank you so much!
alright if it helped, accept the answer for future OP. Your welcome
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.