Epsilon
Epsilon

Reputation: 43

NodeJS - Can I parse and use JSON response body in a new request?

First, please understand that I am new to nodejs, and in fact javascript so temper your response accordingly as I might not be familiar with the concepts that seem very obvious to you.

Is it possible (and if yes, please point me in the right direction) to parse and use the response body from a request.get call in a new request?

For example:

var eventDetails;

function getLatestEvent() {
  var options = {
      url: 'http://localhost:4000/events/get/latest',
      json: true,
      headers: {
          'User-Agent': 'request'
      }
  };
  return new Promise(function(resolve, reject) {
      request.get(options, function(err, resp, body) {
          if (err) {
              reject(err);
          } else {
              resolve(body);
              console.log(body);
          }
      })
  })

}

function resolveLatestEvent() {
  var eventsPromise = getLatestEvent();
  eventsPromise.then(function(result) {
      eventDetails = result;

   // Here I would like to make a new POST request using parts of 
   // the JSON response from the GET request above

   // How can I parse and use the response body in the request,
   // assuming the request looks something like below


   // Send POST request
    const config = { headers: { 'Content-Type': 'application/json' } };

    request.post({
        url: 'http://localhost:3000/events/notify',
        headers: {}, 
        data: {
            title: '',
            team: '',
            day: '',
            creator: '',
            description: '',
            ticket: ''
        }
      }, config);

  // log result
  console.log(eventDetails);
  }, function(err) {
      console.log(err);
  })
}

console.log('Fetching latest event data and making new POST request');
resolveLatestEvent();

Example response body from the GET request:

   [ { title: 'test title',
       team: 'TEST',
       description: 'asd asd asdf sdafsda',
       ticket: 'ABC-12345',
       day: 'Today at 10pm',
       creator: 'user_name' } ] }

Upvotes: 0

Views: 421

Answers (2)

Epsilon
Epsilon

Reputation: 43

The correct solution from my code example above is as follows:

function resolveLatestEvent() {
  var eventsPromise = getLatestEvent();
  eventsPromise.then(function(result) {
      eventDetails = result;

        const config = { headers: { 'Content-Type': 'application/json' } };

        axios({
            method: 'post',
            url: 'http://localhost:3000/events/notify',
            headers: {}, 
            data: {
                title: eventDetails[0].title,
                team: eventDetails[0].team,
                day: eventDetails[0].day,
                creator: eventDetails[0].creator,
                description: eventDetails[0].description,
                jira: eventDetails[0].ticket
            }
        }, config);


    // Log out what we get for debugging
    //console.log('here are the eventDetails', eventDetails)


  }, function(err) {
      console.log(err);
  })
}

Upvotes: 0

As the body is a JavaScript Object you could reference it with a . or with a key ['content'].

So you gonna get the return and use like:

eventsPromise[0].title

or

eventsPromise[0]['title']

As the response is inside an array you should reference the the position you want or just iterate to get to all the positions.

Upvotes: 1

Related Questions