Sending HTTP Requests in Rust Applications 🦀 Rust Tutorial for Developers

Поділитися
Вставка
  • Опубліковано 12 вер 2023
  • There are several different crates available for Rust, that allow you to craft and send HTTP requests to remote servers. The crate we'll be looking at, in this video, is called "reqwest." Reqwest is built on top of another low-level HTTP client crate called "Hyper." You can use both a synchronous or asynchronous approach to handling HTTP requests. To keep things easy to understand in this video, we'll stick with synchronous requests!
    You can customize your HTTP headers, endpoint, port number, and payload (body) in your requests, and process any similar data fields from the response. You can use this technique to interact with any HTTP API that's available, or perform data scraping against various websites.
    🤯 Rust Programming Playlist 🦀 • Rust Programming Tutor...
    📖 Reqwest Docs 📦➡️ docs.rs/reqwest
    Visual Studio Code ➡️ code.visualstudio.com
    Rust Website ➡️ rust-lang.org
    Rustup Installer ➡️ rustup.rs
    Rust Docs ➡️ doc.rust-lang.org/book
    Please follow me on these other social channels!
    ➡️ trevorsullivan.net
    ➡️ github.com/pcgeek86
    ➡️ / pcgeek86
    ➡️ / trevorsullivan
    ➡️ / trevorsoftware
    ➡️ tiktok.com/pcgeek86
    All trademarks, logos and brand names are the property of their respective owners. All company, product and service names used in this website are for identification purposes only. Use of these names,trademarks and brands does not imply endorsement.
    #rustlang #rust #rustdev #opensource #software #linux #devops #programming #rusty #dev #coding #codinglife #code #coder #ubuntu #ubuntulinux #appdev #developer
  • Наука та технологія

КОМЕНТАРІ • 34

  • @ihgnmah
    @ihgnmah 8 місяців тому +9

    One of my favorite things about your videos is that you introduce us to various related tools. I didn't know about Mockoon and was having trouble setting up a proxy to avoid repeatedly hitting a production server. Thank you for sharing your knowledge with us.

    • @TrevorSullivan
      @TrevorSullivan  8 місяців тому +1

      Thank you!! 🙂 I'm glad you are discovering new tools! Mockoon is pretty helpful, right?

    • @ihgnmah
      @ihgnmah 8 місяців тому

      @@TrevorSullivan Yeah, its UX and DX are awesome.

  • @Aras14
    @Aras14 8 місяців тому +8

    A better way to do the check for error/ok is to do a match statement:
    match http_result {
    Ok(res) => {
    //Handling the response
    },
    Err(err) => {
    //Handling the error
    }
    }
    That way you avoid using unwrap so much, which in my opinion looks better, is easily readable and helps reduce panics.
    Edit: It also might be a little more performant.
    Edit2: A good place to use unwrap here is on the client builder, since your program can't work without it. If it fails it shouldn't run.

    • @foofighterdaz
      @foofighterdaz 6 місяців тому

      Thanks for this comment. Great to compare the two approaches.

  • @jmeumeu
    @jmeumeu 9 місяців тому +9

    One thing to keep in mind, is that even if the response is Ok(....), it does not mean that you have a 200 status code. So you probably want to check that response.status() is equal to reqwest::StatusCode::OK

    • @dooterino
      @dooterino 3 місяці тому +1

      response.status().is_success() would probably be a better route, it returns true for all values 200-299 which may be leveraged by an API for alternate success indicators

  • @hectorrios1432
    @hectorrios1432 2 місяці тому

    Great video, thank you! Very clear and easy to follow, which is what we need more of when trying to learn Rust.

  • @xE92vD
    @xE92vD 6 місяців тому

    Thanks. The additional crates you provided (especially mackoon) were very helpful.

  • @dancostello2
    @dancostello2 3 місяці тому

    This was a great tutorial - thank you!

  • @RichardWalterLanda
    @RichardWalterLanda Місяць тому

    top! thnaks yu mate for what yu are doing. I think yu deserve much more attention of rust community! wish yu a good luck

    • @TrevorSullivan
      @TrevorSullivan  Місяць тому

      Thank you, Richard! I appreciate you being part of the community! I'm glad that you're learning from these videos. That's why I produce them. 🦀🦀

  • @symshark
    @symshark 9 місяців тому +1

    This tutorial came just in time. I wasn't familiar with Mockoon but I will be using it going forward to test scripts, thanks!

    • @TrevorSullivan
      @TrevorSullivan  9 місяців тому

      It's a great tool! 🙂 Rust on!! 🦀

  • @alexbrun6863
    @alexbrun6863 5 місяців тому

    absolute legend

  • @raph384
    @raph384 8 місяців тому +1

    best rust tutos i've ever seen really

    • @TrevorSullivan
      @TrevorSullivan  8 місяців тому +1

      That's very kind of you, thank you

    • @raph384
      @raph384 8 місяців тому +1

      but what is your vs code theme? i like the colors@@TrevorSullivan

    • @TrevorSullivan
      @TrevorSullivan  8 місяців тому +1

      @@raph384 it's the outrun theme by samrapdev

  • @farzadmf
    @farzadmf 9 місяців тому +4

    Thank you for the video, and all the videos honestly; I LOVE how you walk through the steps, not skipping any. Very nice job
    One question: you said you're running Rust programs on you Linux box (and I see VSCode is ssh-ing); does it mean that you're running mockoon remotely as well?

    • @TrevorSullivan
      @TrevorSullivan  9 місяців тому +1

      Thanks so much for your kind comment!!
      Mockoon is running locally on my Windows 11 system. That's why I had to use it's mDNS name to connect to it remotely from the Rust code on the Linux VM. I hope this makes sense! Great question!
      By the way, I have another video series on LXD, which is what I use to launch Linux virtual machines for self-hosting and development purposes. You might be interested in that! 🙂

    • @farzadmf
      @farzadmf 9 місяців тому +1

      @@TrevorSullivan will definitely check out the video. I personally use SSH port forwarding for these kinds of use cases. Don't know how to set up mDNS (honestly, had to Google it because didn't have any idea what it is 😆)

    • @TrevorSullivan
      @TrevorSullivan  9 місяців тому +1

      @@farzadmf mDNS is just an easy way to resolve hostnames on a small network. It uses multicast, so you don't have to set up a dedicated DNS server. It's convenient, but has some limitations.
      You shouldn't need to use SSH forwarding since the VSCode remote extension will handle the SSH connection for you. Maybe I'm missing some details in your setup though.
      Rust on! 🦀🙂

    • @farzadmf
      @farzadmf 9 місяців тому

      @@TrevorSullivan Rust on indeed 😆
      As for SSH port-forwarding, my use case is maybe the other way around: a client on my local machine needs to access a server running on my remote SSH server; that's where I use port forwarding to accomplish that
      TBH, I've been what I call a "Rust watcher" for a long time, but your tutorials are so well done that encourage me to actually do something with Rust 😉
      One personal suggestion: it would be nice to start "putting things into practice" and create some project-based series; I'd personally love that/those 🙂

    • @TrevorSullivan
      @TrevorSullivan  9 місяців тому +1

      @@farzadmf oh that makes sense! Using SSH as a transport security layer is excellent for accessing remote services!
      Thank you for your comment about my videos! I needed a push myself as well, since I'm not accustomed to working with low-level languages. Rust has really bridged the gap though, and I'm excited to learn about it!
      Project based videos are a great idea as well! I'll eventually get to something like that, where we "assemble all the pieces" and make a useful application. At the moment, I'm trying to take small bites before I dive too far into a whole app. 🙂

  • @xieen7976
    @xieen7976 2 місяці тому

    can you do a example of stream post and handle chunked json response?

  • @user-xd8pl8vw8g
    @user-xd8pl8vw8g 5 місяців тому

    Hi Trevor, thank you for providing this tutorial. By the way, may I ask you a question? How did you fix that TLS error of OpenSSL and pkgconf? Thank you.

    • @TrevorSullivan
      @TrevorSullivan  5 місяців тому +1

      Hello! I installed libssl-dev and pkg-config with the apt package manager!

    • @user-xd8pl8vw8g
      @user-xd8pl8vw8g 5 місяців тому

      @@TrevorSullivanThank you for your response sir.

  • @user-jf7wh4zw5y
    @user-jf7wh4zw5y 8 місяців тому

    Why not using a proper HTTP mock library instead of Mockoon? In any case, you want to have your tests automated to make sure there are no breaking changes in the future. I don't see how this can be achieved with Mockoon.