Skip to content

Instantly share code, notes, and snippets.

Last active Feb 14, 2022
What would you like to do?
Create web service that sends a request to github api
name = "rocket_reqw"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at
rocket = "0.4"
serde = {version = "1", features = ["derive"]}
serde_json = "1"
reqwest = {version = "0.11", features = ["blocking", "native-tls", "json"]}
// Author: Ugo Varetto
// Updated version of:
// Also returns JSON record for valid users.
use rocket::http::RawStr;
use reqwest::blocking::Client;
use reqwest;
use std::time::Duration;
use serde_json;
use serde_json::Value;
#[macro_use] extern crate rocket;
fn index() -> &'static str {
"Navigate to http://localhost:8000/check/<type your GitHub name here> to check that everything is working!"
fn check(user: &RawStr) -> Result<String, Box<dyn std::error::Error>> {
let request_url = format!("{}", user);
let client = Client::builder()
.timeout(Duration::new(5, 0))
// without user agent it returns 403
.user_agent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.81 Safari/537.36")
let resp = client.get(request_url).send()?;
if resp.status().is_success() {
let pp = if let Ok(r) = resp.text() {
let j: Value = serde_json::from_str(r.as_str())?;
} else {String::new()};
Ok(format!("\"{}\" is a user\n\n{}", user, pp))
else { Ok(format!("\"{}\" is NOT a user", user))}
fn main() -> Result<(), Box<dyn std::error::Error>> {
rocket::ignite().mount("/", routes![index, check]).launch();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment