Skip to content

Commit

Permalink
feat(std): support body param in http requests
Browse files Browse the repository at this point in the history
  • Loading branch information
AlenVelocity committed Mar 28, 2022
1 parent 75f3381 commit 3938c3f
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/std_library/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ pub fn add_globals() -> Res {
}

pub fn request(args: Vec<Object>) -> Object {
if args.len() < 2 || args.len() > 3 {
if args.len() < 2 || args.len() > 4 {
return Object::Error(format!(
"Wrong number of arguments. Got {}. Expected 2 or 3.",
"Wrong number of arguments. Got {}. Expected 2-4.",
args.len()
));
}
Expand Down Expand Up @@ -48,10 +48,15 @@ pub fn request(args: Vec<Object>) -> Object {

let client = reqwest::blocking::Client::new();

let body: String = match &args[3] {
Object::String(s) => s.clone(),
o=> o.to_string(),
};

let response = match method.as_str() {
"GET" => client.get(url).headers(headers).send(),
"POST" => client.post(url).headers(headers).send(),
"PUT" => client.put(url).headers(headers).send(),
"POST" => client.post(url).headers(headers).body::<String>(body).send(),
"PUT" => client.put(url).headers(headers).body::<String>(body).send(),
"DELETE" => client.delete(url).headers(headers).send(),
_ => return Object::Error(format!("Unsupported HTTP method {}", method)),
};
Expand Down

0 comments on commit 3938c3f

Please sign in to comment.