rl_lang/interpreter/stdlib/http/
http_post.rs1use crate::{
2 interpreter::{
3 evaluator::Evaluator,
4 stdlib::{
5 common::{check_arity_range, extract_string, verr, vs},
6 http::common::ureq_result_to_value,
7 },
8 values::Value,
9 },
10 utils::{errors::Error, span::Span},
11};
12
13pub fn func(_: &mut Evaluator, args: Vec<Value>, span: Span) -> Result<Value, Error> {
14 check_arity_range(&args, 2, 3, "http_post", span)?;
15
16 let url = match extract_string(args[0].clone(), "http_post") {
17 Ok(s) => s,
18 Err(e) => return Ok(verr!(vs!(format!("http_post: {e}")))),
19 };
20 let body = match extract_string(args[1].clone(), "http_post") {
21 Ok(s) => s,
22 Err(e) => return Ok(verr!(vs!(format!("http_post: {e}")))),
23 };
24
25 let content_type = match args.get(2) {
26 Some(Value::String(s)) => s.clone(),
27 Some(other) => {
28 return Ok(verr!(vs!(format!(
29 "http_post: expects a string content_type, got {}",
30 other.type_name()
31 ))));
32 }
33 None => "text/plain".to_string(),
34 };
35
36 let result = ureq::post(&url)
37 .set("Content-Type", &content_type)
38 .send_string(&body);
39 Ok(ureq_result_to_value(&url, result))
40}