This episode is for members only

Sign up to access "Build An Uptime Monitor with Inertia" right now.

Get started
Already a member? Sign in to continue
Playing
22. Making an HTTP request

Episodes

0%
Your progress
  • Total: 4h 59m
  • Played: 0m
  • Remaining: 4h 59m
Join or sign in to track your progress

Transcript

00:00
Laravel has an HTTP client wrapper around Guzzle, which allows us to very, very easily make requests. And as you'd imagine with Laravel,
00:08
it's also very nice to use. So we're going to go ahead and use this. And this is actually going to be incredibly simple, because all we're really doing here
00:17
is making a GET request to these particular endpoints. Of course, later on, if you wanted to add the ability to make a POST or PATCH request through, you could add columns in here to specify the type.
00:30
We may get to that a little bit later. But for now, we just want to make a GET request to the location here. And of course, making sure we add on the domain name
00:39
that we're sending the request to as well. So let's get started. And we're not going to store our checks anywhere just yet, because we don't have a table for that at the moment.
00:46
But we'll just dump some of the data out just to see what we get. So the first thing I'm going to do really importantly is add this around to TRICATCH.
00:54
So we're going to go ahead and catch any general exception here. And we're actually going to do nothing, because when we make the request here
01:02
and we get the response back from the HTTP client, this is going to tell us whether the request was successful. We just want to catch any exception here just in case something does go wrong and we don't want
01:13
to completely break the job. So to use the HTTP client, we just use the facade here. And of course, we're just making a GET request. So we're just going to use the GET method.
01:24
And then we want to grab the full URL from the endpoint. Now, at the moment, we don't have this in here. We have the site, so we could access that relationship and add that on.
01:34
But why don't we just do this in a method? It's probably going to make it a little bit easier. So let's go ahead and create our URL method on our endpoint. And we're going to go ahead and reference the site URL, which
01:47
we implemented a little bit earlier. We just have a look here. That's this one here. And then we're going to go ahead and concatenate on the location.
01:57
So now this URL should give us, if we head over here, the entire URL that we want to make a request to. So we can now just say endpoint URL. Now, that is pretty much all we're
02:08
doing to make a request to the endpoint. The beauty in this is the data that we actually get back from this. So let's just go ahead and dump out some of the data
02:19
that we get back from the response. So we're going to go ahead and say response status, first of all. Let's try that one first. Now, at the moment, I'm not sure when these next checks are.
02:28
But let's go ahead and just run the command anyway and just see what we get. There we go. We get a 404.
02:34
Now, that is probably for slash pricing, because that doesn't exist over on CodeCourse. Let's just change this over to pro and this one over to forward slash, which both do exist.
02:45
And let's set the next check time to now, just so we can speed this along. Let's add both of these now. Obviously, we won't get to this one, because we're
02:54
dying and dumping here. Let's go ahead and do that. And sure enough, we get a 200. So this is working nicely.
02:59
And we're getting back the data we want to be able to determine whether it was successful. Now, we also have things like a successful method. So if we just go ahead and run this, we get true, of course.
03:10
And we also have things like body, as well, which is going to give us back the response we get from this request, which, as we know from the introduction, we're going to store when we get an unsuccessful request.
03:20
So as long as we have a successful request, we're not going to store the body. But if it's unsuccessful, we are going to store this body. So that is pretty much it for the HTTP request.
03:31
We may have originally thought it would be a lot more difficult than this. But of course, with Laravel, everything is nicely wrapped and really, really simple to use.

Episode summary

In this episode, we dive into how easy it is to make an HTTP request using Laravel's built-in HTTP client, which is just a nice wrapper around Guzzle. We start by setting up a simple GET request to a specific endpoint. If you ever want to support other request types like POST or PATCH, you can totally extend the approach later, but for now, we're sticking with GET requests.

We write out a small method to generate the full URL for each endpoint by combining the site's domain with the desired location. Before we integrate full error handling and data storage, we keep it simple — just dumping the response status to see what we get back from our requests. We see what happens when we hit a non-existent endpoint (getting a 404), and then adjust the URLs to ensure we get a 200 status for existing resources.

You’ll get a peek at how easy it is to access not just the HTTP status code, but also check if the request was successful and even grab the response body. Although we're not saving the data to the database yet, this lays the groundwork for later when we'll log failed bodies for debugging.

Basically, Laravel makes HTTP stuff super straightforward, and you'll see that making requests is a breeze!

Episode discussion

No comments, yet. Be the first!