Angular.js $http request data from Propublica Congress API Part 1

This is a short example of how to get data back from the ProPublica Congress API using Angular’s $http service on the front end of an app.

Get data from api with $http

The first thing I want to do in this app is get a list of all the state senators in congress. I used the ProPublica api docs to find the correct endpoint to get all senators. I am able to use Angular’s $http service to send a GET request for data using this endpoint https://api.propublica.org/congress/v1/115/senate/members.json found in the api’s documentation. The $http service is a great service provided by Angular. The alternative to $http is using a node.js server to send a get request which I found to be a difficult way to get and render data on the front end.

//You will need to submit a request for an 
//api key at https://www.propublica.org/datastore/api/propublica-congress-api 
function mainContainerCtrl($http) {

    var self = this;

    $http({
        method: 'get',
        url: 'https://api.propublica.org/congress/v1/115/senate/members.json',
        headers: { 'X-API-KEY': 'fakekey123xcvbnm123456999' }
    }).then(function(response) {
        //Get data from the response and print it to the console.
        console.log(response.data.results[0].members, ' members');
        //Define the array of data for ng-repeat in the html
        self.senators = response.data.results[0].members;
    }).catch(function(error) {
        //If error throw error
        console.error("Error with GET request", error);
    })

}

This is a breakdown of the above code. The $http service requires me to define what kind of http method this is in this case it’s a GET request. Then I must define the endpoint (url) in where to send this get request to. ProPublica requires the api key to be sent with the headers option and it must be exactly like this 'X-API-KEY': 'fakekey123xcvbnm123456999' in order to get data sent back to my app. If the $http service gets the data successfully from the api then() is called and the promise is fulfilled and the data is past in as a response (response = data object). If the $http service can not get the data back from the api the code will throw an error. To access the senator property inside the response object that came back from the api I logged the object to the js console in chrome. This console log helped me find the array of senators console.log(response.data.results[0].members), now I can see the array of senators in the js console of the chrome browser. This code means response.data.results[0].members in the response object there is a data object in the data object there is a results object in the results object there is an array with a key of members which is the array of senator objects and that is what I want ng-repeat to iterate over in the html. This array is then defined to a varibale called self.senators so it can be rendered in the html with ng-repeat as vm.senators.

<!-- main-container.html -->

<ul ng-repeat="s in vm.senators">
  <!-- construct a string to show some of the values from the keys found in vm.senators -->
  <li>{{s.first_name + ' ' + s.last_name + "("+s.party+"-"+s.state+")"}}</li>
</ul>

To have a better look of the data object in this case vm.senators use the <pre> tag in the html file.

 <pre>
    {{vm.senators | json}}
 </pre>

Check the codebase on my GitHub account!