A Very Simple API Call

Поделиться
HTML-код
  • Опубликовано: 4 фев 2025
  • If you've never worked with APIs they can seem a bit complicated and confusing. In this 2 minute tutorial I walk you through the basics of a very simple GET call in a RESTful API.
    Be sure to pop over to uxapi.io/howto... where you can play with the live console I mention in this video.
    -----
    There are basically three things you need to read data from an API
    Method:
    The "method" which is also called a "verb" tells the API what you're doing:
    GET: Look at an API object or collection of objects
    POST: Create a new object with the payload being sent
    PUT: Overwrite an object with the data being sent
    DELETE: Delete the object
    Path
    The path is the location of that API object or collection of objects. This is like a web address, usually we have a full path like
    api.uxapi.io/v...
    But if you're working on a local copy it might look like
    /v1/blogs?cateogry=API%20Basics&size=2
    Note the query parameters for category and size; sometimes we use query params to tell the API to do things like filter the results, or how to sort the response (e.g. `sort=desc`)
    We use the method and the path to call to the API with things like
    GET /v1/blogs to look at a list of blogs
    POST /v1/dogs to create a new blog posting
    PUT /v1/dogs/123-abc-456 to update a specific blog posting
    DELETE /v1/dogs/123-abc-456 to delete the blog posting
    Headers
    Headers are part of the HTTP specification and are used in any web traffic like when you're looking at pictures of cats on your favorite social media platform. Unlike query parameters (like in the path example above), headers are hidden from the consumer of the request.
    Most of the time when you're just looking at web pages and images you don't need headers, but an API is not just a web object; it's a dynamic data source that has security rules and other functionality that we can control through the headers.
    While there can be a lot of different kinds of headers, there are a few you're going to see over and over:
    Authorization:
    The `Authorization` header allows you to send either a username and password (somewhat) with `Authorization: Basic {base64 encoded username and pass}` or to send an OAuth or OIDC token as `Authorization: Bearer {your token}`. This is probably the most common header you'll need to apply to all your API calls.
    Accept:
    The `Accept` header tells the API what format you would like to see the object returned in. You might send `Accept: application/json` or `Accept: text/xml` to have the API format the data. The API has to support this kind of functionality, but if it does it can be useful to support different kinds of applications and frameworks.
    Content-type:
    When you're sending a payload with a `POST` or `PUT` you need to tell the API what format your payload is. These are the same formats we use in the `Accept` header, so if you're POSTing some json data, you need to include the `Content-type: application/json` header.

Комментарии • 2

  • @JuanGGZ
    @JuanGGZ 2 месяца назад +1

    Thanks for this, easiest and most practical way to remember how to do a call when you didn't for years! 🙏

  • @rahulkanna1047
    @rahulkanna1047 9 месяцев назад

    link not working