Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 2765

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T08:26:08+00:00 2024-11-26T08:26:08+00:00

A Beginners Introduction to Making API Requests (Unsplash API)

  • 61k

So if you’ve followed upon the previous post where we discussed about Response, then this article about how to implement request and handle response with the Unsplash API would prove very useful in understanding the two concepts. This article by the way assumes you have a basic knowledge of JavaScript and its core concepts such as: Functions and the JavaScript fetch API. But if you don’t know, just read on, I’ll try to explain the steps as much as possible

requests
Now, to make a request in JavaScript, we have a lot of options, we could do an XHR request, or we could use a package called Axios to make a Promise-based request to a web server if you working with a node-generated project. But for the sake of simplicity in this article, we would be using the JavaScript fetch API. The Fetch API basically allows us to make HTTP requests to web servers. We could use Fetch on its own to make a request to our API and then tackle on the response using a .then() function, or we could use a cleaner method of wrapping our Fetch in an async function. Nevertheless, this article is not focused on teaching you how to write async functions, rather how to use it to make our requests easier with Fetch.

Having talked at length about making requests to an API using Fetch, you may wonder what an API exactly is: In short an API (Application Programming Interface) is any standard interface or endpoint that facilitates connection to a web server. In this article we would be making a simple request to Unsplash API. Unsplash is a web platform that acts as a repository of high quality free images, hence, their API enables us to make requests to their central server for an image, which would in turn make a response to us.

Making a Request:
To get started, quickly scaffold a simple index.html project on your PC. It should look something similar to this:

scaffold image
So at the script section of our project we are going to create two things: a fetch request without the async function as a wrapper and also a fetch request with the wrapper. This way we can get a knowledge of both worlds:

requests
Now looking closely, the searchTerm and the accessKeys are two variables that would help our API below them function optimally. Access Keys are private hence endeavor to keep them hidden!
In fetch without async wrapper above, we use the fetch to make a GET request to a server(Unsplash) via an API, this request is then tackled on using the .then() methods. The first one parses the response gotten from the server (res), converting it to a more usable “JSON” format; This enables us to access the response the same way we would with a JavaScript objects. Also, the second .then() method grabs the parsed data and outputs it on our browsers console.
The async function too also helps in making asynchronous requests, how so: a response is expected and upon getting the response the data is stored in a constant called – res. Next we await the response to be parsed into a JSON format, after resolving it is passed to a constant – data.
One thing to note is that these operations occur in a non-blocking manner, they execute sequentially such that the fetch must run/execute and get a response before the response is then parsed, after which we then output or use the parsed data. Such is the nature of asynchronous JavaScript.
If you’ve followed this article properly, you should come to the understanding of not the syntax for making a GET request to an API using fetch, but rather of how making requests asynchronously works in JavaScript. Handling Responses and using them on our page is also vital to understand. The part2 of this article series would explain not only how to handle and use responses but also how to structure our requests to catch and handle errors too, should they occur. Happy Learning!

beginnersjavascriptwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.