Making requests in Rust


English (Auto-generated)

well, everyone as you can probably tell from the title of this one. We're gonna be doing requests. Um if you're coming basically what this video is about is for people who are coming from higher level languages, you know, python, javascript or what have you who are used to requests being this simple thing and you start learning rust and the easiest simplest features are suddenly insanely difficult. Well, at least it appears that way when you're not used to rusts way of doing things. Being a systems language, you're often operating with these lower level considerations in mind. That can be intimidating for newcomers. But once you get the hang of it, it's really not a hindrance at all. Mhm. Nevertheless, let's take a look. So first things first we have our cargo dot kamil we have to import the requests library. Okay. You see that we are getting version 11 and we need this blocking feature. Okay. You will see why we need blocking specifically in a moment. Basically, it's to make a synchronous version of the request. Like in other words, blocking other other functionality in the code from running. Alright, now let's get to our main dot rs We have a little Hello world here and start coding. So yeah, the first thing we're gonna do is like we already have our main function. So let's make a request. It will say let request equal. Okay, we have our little Ai thing uh my get up. Co pilots already recommending some code. Okay. But it's actually not what I want to do. We're gonna do a get call but we wanted to be synchronous. So we called get first and I will get the site. I can HAZ I. P. You might wonder why that one it's just basically a site that has nothing on it other than some plain text. Which makes it great for this sort of thing because it's really easy to see if it worked right? It will show us our I. P. Or not. That's that's exactly what we're gonna do. We're gonna try to look at our I. P. Address. Okay? Um So you see this question mark I'm having at the end so I'm trying to have it so it's gonna return a result. In other words this might be an error because of that. I'm gonna have to consider that it could return nothing but it also could return an error. So we're going to have to account for that inside of main flexible. Okay so we're saying this could be an error, we're gonna handle that. What else do we need to do? Well, okay we've made the request, we don't want to print Hello world anymore but we do want to get the body and that's just gonna be a stream, yep just a new string that's where we're gonna store the result of this request. In other words and again you will see why in a moment. I promise. Mhm. Alright. It thinks that we're going to do body request dot text. It's a good guess but we're not going to do that. We're going to read it. We're going to read the response to a string. I called this request. I meant to call it response. So we will say our response. We wanted to read to a string well past the body and that's a string region. That means it needs to be mutable. Just a comparing python. What would this be in python? We would just say import requests. I would say, you know, our response equals requests. Uh Okay. And then we would say, you know, print Yeah are dot tex. Not quite so easy and rest. But we're almost ready at this point. We can actually print out the body and one final thing we need to say, hey this function is uh the results. So we want that result to come out successfully. We're gonna say, okay, if we made it here. No error. Woo hoo! No. Wonderful. So let's try running our code. We will use the command cargo run and oh no. So this error isn't found inside of this scope. Like I'm returning this error and it's like well what the heck is that? So let me to say use standard error. And from there we can import this error. Beautiful. Let's try it again. Clear so that we can more clearly see it if we get another error which we do. So it says right here use STD io dot read. And being the great engineer. I am. I'm not even gonna think about why that's necessary. Let's just paste it on in there and let's read that. It can help items from. Creative only views that the treat is in the scope so it looks like read to string uses some feature of read and it needs that trait in the current scope to work. So we'll try cargo run again. Alright we're making some progress, making some progress says right here helped consider changing this to be mutable mute rez Beautiful. Okay mute reds. Let's give it one more shot. Okay I kinda just froze there. I think I intimidated it. There we go. And pretty close pretty close. Let's see error request kind builder relative U. R. L. Without base. So we need a schema. It's thinking that like the current U. R. L. Slash that or something like that. Once for you are L. Schema. And we got our I. P. Address. That's my I. P. Address. If anyone out there is trying to detox me. Uh Congratulations you have successfully stocked and found out where I am. But yeah, there you go. I hope that was helpful. Um The beginners out there, you might be like well I'm totally lost. Don't worry about it. The purpose of this video is to show you that requests and things that you might think of as simple are a little more complicated than rust. That's not because rust is complicated. It's because those tasks are actually complicated and rust just hides that from you a little less than other languages do. Um, well, I hope it's been educational. Thank you a ton for your time, and I look forward to seeing you all next time. Bye bye.
11 Views 0 Likes 0 Comments

A systems programming language like Rust often involves a lot of complexity for tasks that are normally simple in higher level languages like Python and JavaScript. To demonstrate this, let's look at how Rust does web requests

Leave a comment (supports markdown format)