ga-wdi-exercises / pbj-project3

[project]
0 stars 2 forks source link

API request request header issue #10

Closed edwardpark closed 9 years ago

edwardpark commented 9 years ago

we are trying to call a external api in our app.js file and are using the nom request module. however we are confused how to include the api key into the header for it to work.

right now below is the code we are using and have required and install npm request but keep receiving the error "statusCode": 404, "message": "Resource not found"

we have also tried inserting the api_key into the url to no avail.

request({
  uri: "https://api.wmata.com/NextBusService.svc/json/jPredictions&1001195",
  method: "GET",
  timeout: 10000,
  xhr.setRequestHeader(api_key, "fa310ecaa39a489db9e3ba0e4978d115"),
  followRedirect: true,
  maxRedirects: 10
}, function(error, response, body) {
  console.log(body);
});
RobertAKARobin commented 9 years ago

Hm. What is this Nom Request Module of which you speak? You mean the Node Request Module?

jshawl commented 9 years ago

check out the documentation about including custom headers - https://github.com/request/request#custom-http-headers

edwardpark commented 9 years ago

ok just tried the github doc but response is not showing anything now which i suppose is a improvement !

RobertAKARobin commented 9 years ago

Try console.log([error, response, body]); There has to be information in one of those three.

edwardpark commented 9 years ago

ok still coming up as a 404

RobertAKARobin commented 9 years ago

What does the &1001195 at the end of the URL do?

Since this is a GET request, I can visit it in my browser. With the &1001195 it throws a 404; without, it just says I'm missing an API key.

RobertAKARobin commented 9 years ago

Incidentally, you may want to remove your API key from up there. Don't want any sneaky scrapers to nab it!

edwardpark commented 9 years ago

took it from the WMTA query instructions

Request URL

https://api.wmata.com/NextBusService.svc/json/jPredictions[?StopID]

lol yeah just trying to get it to work right now

edwardpark commented 9 years ago

1001195 is a bus stop number

RobertAKARobin commented 9 years ago

Gotcha. Your URL should look something like this:

https://api.wmata.com/NextBusService.svc/json/jPredictions?StopID=1001195&api_key=[key]

There has to be a ? before the start of the querystring; & separates the different items inside it. Also, it looks like you need to actually include "StopID" in the URL. Not very clear on the WMATA site.

The URL above gave me this:

{
"StopName": "7th St + Massachusetts Ave",
"Predictions": [
{
"RouteID": "70",
"DirectionText": "North to Silver Spring Station",
"DirectionNum": "0",
"Minutes": 3,
"VehicleID": "6405",
"TripID": "7725831"
},
{
"RouteID": "70",
"DirectionText": "North to Silver Spring Station",
"DirectionNum": "0",
"Minutes": 8,
"VehicleID": "6362",
"TripID": "7725830"
},
{
"RouteID": "74",
"DirectionText": "North to Convention Center",
"DirectionNum": "0",
"Minutes": 16,
"VehicleID": "3008",
"TripID": "7726506"
},
{
"RouteID": "70",
"DirectionText": "North to Silver Spring Station",
"DirectionNum": "0",
"Minutes": 18,
"VehicleID": "5432",
"TripID": "7725829"
},
{
"RouteID": "74",
"DirectionText": "North to Convention Center",
"DirectionNum": "0",
"Minutes": 33,
"VehicleID": "3006",
"TripID": "7726505"
},
{
"RouteID": "70",
"DirectionText": "North to Silver Spring Station",
"DirectionNum": "0",
"Minutes": 43,
"VehicleID": "6404",
"TripID": "7725827"
},
{
"RouteID": "70",
"DirectionText": "North to Silver Spring Station",
"DirectionNum": "0",
"Minutes": 55,
"VehicleID": "5443",
"TripID": "7725826"
},
{
"RouteID": "74",
"DirectionText": "North to Convention Center",
"DirectionNum": "0",
"Minutes": 75,
"VehicleID": "3008",
"TripID": "7726503"
},
{
"RouteID": "74",
"DirectionText": "North to Convention Center",
"DirectionNum": "0",
"Minutes": 95,
"VehicleID": "3006",
"TripID": "7726502"
}
]
}
kscoulter commented 9 years ago

Our key keeps getting denied. How did you get that response?

RobertAKARobin commented 9 years ago

Just pasted it into my browser's address bar! I used the key you put up above.

edwardpark commented 9 years ago

oh dear lord, ok just figured it out LOL thanks!