Monday 20 May 2019

n00b help with a loop/filter/log for HTTP requests

Hi all, I'm a node.js noob and I'm trying to write a script that involves looping, filtering and storing information and I'm getting a little stuck, so I thought I would ask for some help here. I have a math/scientific computing background so I'm not totally new to programming, but I'm a (very) junior developer when it comes to JS.Basically, I am doing some data mapping to identify issues in a CRM system. I've been tasked with finding contacts with specific information and filtering the results (I have to find clients who have been labeled 'deceased' and then figure out why users can't filter them.) Originally, I tried just manually making requests in Postman, but that method is a) very tedious and b) it would be good for me to figure out how to do it with a script (and it would make my superiors happy.) Unfortunately, I can't provide too much information on specifics, due to privacy issues. So, my work-around is to base my questions on the Star Wars API, because it will essentially work the same as what I am doing (in fact, I am going to design my script with the SWAPI, then once I have it working, I will put the actual URL and authentication headers in, and run it "for real".) I am using Axios to make GET requests.So basically I have (3) problems I need to tackle.1) I need to filter the responses based on things I can't search with the API; I have to pull the data from the response body. So, using my SWAPI example, I would need to filter characters with eye_color: blue in the response (see below.)2) I need to log the response body (or the ID number, at minimum, so I can retrieve it manually later) if it contains the data I am interested in. To stick with my example, I need to log name: 'Luke Skywalker', since this character contains eye_color: blue in the response.3) Finally, I need to make this process iterative so I can cycle through hundreds of contacts. (I figure I will tackle this last, once I have the filtering and logging worked out in my script.) So, in my SWAPI example, I would need to do something that iterates to get data from different characters, /people/{i} for (i={initialID};i={finalID}; i++) or something like that. It would be cool if I could have the loop stop when I get a null contact, but honestly I could manually find the start and end numbers for the contacts I'm interested in, and just loop for those.My script looks like this right now:var axios = require (\'axios\'); var URL = "https://swapi.co/api"; try{ axios.get(URL+\'/people/1\') .then(function(response) { console.log(response.data); }) } catch (err) { alert(err);}; https://pastebin.com/embed_js/jGaJBeUxI also have a script using async/await that I could use, but I don't think it really matters other than async/await is a little cleaner.And the part of the response I am interested in looks like this:name: \'Luke Skywalker\', height: \'172\', mass: \'77\', hair_color: \'blond\', skin_color: \'fair\', eye_color: \'blue\', birth_year: \'19BBY\', gender: \'male\', https://pastebin.com/embed_js/aXcRikYcIf anyone can offer some help, that would be great. I have been piecing together methods from various places I've found googling, but I'm struggling to make sense of a way to implement all of the above in a way that works for my particular issue. I will edit my post to include other information if I need to. This is my first post here so if I screwed something up, just let me know. Thanks!

Submitted May 20, 2019 at 09:15PM by railnruts

No comments:

Post a Comment