I am currently watching a YouTube series on API queries, and the instructor is using the p5 library. I have never used this library, and I was looking on some feedback from other campers who have already gone through the intermediate Front End Development challenges.
My questions:
- Do you use libraries to help you with data queries?
- If you do use a library which one? Why?
- Is there an industry standard when it comes to using libraries for this? This guy makes it seem like it’s the end all be all of libraries.
- Do you have any learning material(videos, documentation,etc.) that you found beneficial for this topic that you could share?
I haven’t heard of p5. Previously, I’ve used jQuery’s $.ajax
and $.getJSON
methods, as well as the native JavaScript XmlHTTPRequest
constructor.
However, as long as you only need to support modern browsers, there’s really only one sensible choice these days: the fetch
API, which uses Promises.
For a simple GET request to an API that returns a JSON:
fetch('https://myfancysite.com/api?format=json')
.then(data => data.json())
.then(json => doSomethingCoolWith(json));
Simple as that!
2 Likes
You seems to be learning how to implement API’s and get their response in JSON. I would suggest you not to simply start by using libraries as they contains premade codes which you have to call and your work is done.
As a beginner you should learn to implement APIs on core languages like jQuery, JavaScript, php, asp dot net, python.
-
I prefer PHP when implementing API. PHP is server side language which is easy to learn. It is very secure.
-
If you want to implement API on client side scripts like jQuery then use jQuery AJAX method for it. Implementing API’s in jQuery is very easy and fast but there is only one project which is - you have to keep the API keys on the web page which anyone can see.
However you can also implement API in the language of your choice. Once you understand how to implement API in core languages then you can use library if you want.
JQuery is a JavaScript library, it’s not a language, it’s just a collection of useful ‘premade codes’ for front end JavaScript @Loveaanthony
ASP .Net is a framework which is entirely ‘premade codes’ as well.
Implementing an API is completely different to making requests against an API; OP is taking about making requests, so talking about PHP/ASP/Python etc isn’t relevant.
2 Likes
A lot of good answers. I might add …
There are sites like jsonplaceholder that have dummy data with which you can mess around. Here are some sample calls:
let url = 'https://jsonplaceholder.typicode.com/users'
// *************
fetch(url)
.then(response => response.json())
.then(json => {
console.log('Results from fetch...', json)
})
// *************
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.onload = function() {
if (request.status >= 200 && request.status < 400) {
// Success!
var json = JSON.parse(request.responseText);
console.log('Results from XMLHttpRequest...', json)
} else {
console.log('Error from XMLHttpRequest')
}
}
request.onerror = function() {
console.log('Error from XMLHttpRequest')
}
request.send()
// *************
// requires jQuery
$.getJSON(url, function(json){
console.log('Results from getJSON...', json)
})
Here is a pen with that code.
There are many ways to do it. I’d just start messing around with it, making calls and seeing what you get back. If you want to get fancy, you can start messing around with postman.
APIs were confusing at first. To make matters worse, they are all a little different, often aren’t well documented, and can change without warning. And I don’t think I really fully understood APIs until I started building my own in the backend section. But with some work I understood it well enough.