0

This is probably a super-easy answer for most folks, but I'm new to back-end. I'm using Express.js to implement my server, and I need to retrieve courses from the edX API. Below is the sample request they have on their docs:

curl -X GET -H "Authorization: JWT {access token}" https://api.edx.org/catalog/v1/catalogs/

and I can't seem to find a starting point on how to translate this to Express.js implementations. I was able to perform similar requests to another API using 'request-promise'), but this is the first that requires a JWT Auth...

Docs here: http://course-catalog-api-guide.readthedocs.io/en/latest/course_catalog/catalog.html#get-a-list-of-all-course-catalogs

Thanks a ton in advance!

Jbbae
  • 824
  • 9
  • 19

2 Answers2

2

How can we perform a request with HTTP AUTH ?

From this Github Issue : Bearer Token

Please have a look at the Request documentation. Request-Promise works the same.

With the request module:

request.get('https://some.server.com/',{"auth":{"bearer":"theToken"}})

With the request-promise module:

We know it works as request so we could build the options like this :

var options = {
 "url":APIURL,
 "headers":{"User-Agent":"client"},
 "auth":{"bearer":ACCESSTOKEN}
}

So now you can even create a route in your express app, that performs the query using a client-side token :

app.get("/query-with-token/:accessToken",(req,res)=>{
 var accessToken = req.params.accessToken
 if(accessToken){
   console.log(accessToken);
   RequestAPI((error, apiResponse)=>{ //this would be a custom function of yours
     if(!error){res.send(apiResponse)}
     else{res.send("ERROR: "+error)}
   })
 }else{res.send("no token supplied")}
})

(this approach is not recommended for security reasons)

EMX
  • 6,066
  • 1
  • 25
  • 32
0

Turns out it was super similar to the above, but with a different setup (using the request-promise library):

request.get('https://api.edx.org/catalog/v1/catalogs/', {
 "url":APIURL,
 "headers":{
    "Authorization": "JWT {access token}",
    "User-Agent": "client"
  }
});

Source: Using an authorization header with Fetch in React Native

Jbbae
  • 824
  • 9
  • 19