Created
April 8, 2018 09:17
-
-
Save newton-migosi/fa455586c47e1b16a203c117bdc78d3b to your computer and use it in GitHub Desktop.
Rust API call on Africa's Talking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate reqwest; | |
#[macro_use] | |
extern crate hyper; | |
use hyper::header::{Accept, Headers}; | |
header! {(Apikey, "apikey") => [String]} | |
fn send_request(url: &str, apikey: String) -> Result<reqwest::Response, reqwest::Error> { | |
let mut headers = Headers::new(); | |
headers.set(Accept::json()); | |
headers.set(Apikey(apikey)); | |
let client = reqwest::Client::new(); | |
let resp = client.get(url).headers(headers).send(); | |
resp | |
} | |
fn main(){ | |
let apikey = String::from("d2cb5a86944fd45f279e8dba28c693c65801ba6ea0840df1ac625f6e08b88a7e"); | |
let url = "http://api.sandbox.africastalking.com/version1/user?username=sandbox"; | |
let mut resp = String::from(""); | |
let err_msg = String::from("Error handling request."); | |
resp = match send_request(url, apikey).unwrap().text() { | |
Ok(txt) => txt, | |
Err(_) => err_msg | |
}; | |
println!("{}", resp); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment