mirror of
https://github.com/nushell/nushell
synced 2024-12-28 22:13:10 +00:00
add custom header ability to post command (#4558)
This commit is contained in:
parent
2ecae0ef43
commit
b92aaf0432
1 changed files with 64 additions and 0 deletions
|
@ -10,6 +10,7 @@ use std::str::FromStr;
|
||||||
use nu_protocol::{
|
use nu_protocol::{
|
||||||
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Value,
|
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Value,
|
||||||
};
|
};
|
||||||
|
use std::collections::HashMap;
|
||||||
use std::io::{BufRead, BufReader, Read};
|
use std::io::{BufRead, BufReader, Read};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
|
@ -49,6 +50,12 @@ impl Command for SubCommand {
|
||||||
"the length of the content being posted",
|
"the length of the content being posted",
|
||||||
Some('l'),
|
Some('l'),
|
||||||
)
|
)
|
||||||
|
.named(
|
||||||
|
"headers",
|
||||||
|
SyntaxShape::Any,
|
||||||
|
"custom headers you want to add ",
|
||||||
|
Some('H'),
|
||||||
|
)
|
||||||
.switch(
|
.switch(
|
||||||
"raw",
|
"raw",
|
||||||
"return values as a string instead of a table",
|
"return values as a string instead of a table",
|
||||||
|
@ -86,6 +93,11 @@ impl Command for SubCommand {
|
||||||
example: "post -u myuser -p mypass url.com 'body'",
|
example: "post -u myuser -p mypass url.com 'body'",
|
||||||
result: None,
|
result: None,
|
||||||
},
|
},
|
||||||
|
Example {
|
||||||
|
description: "Post content to url.com, with custom header",
|
||||||
|
example: "post -H [my-header-key my-header-value] url.com",
|
||||||
|
result: None,
|
||||||
|
},
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -93,6 +105,7 @@ impl Command for SubCommand {
|
||||||
struct Arguments {
|
struct Arguments {
|
||||||
path: Option<Value>,
|
path: Option<Value>,
|
||||||
body: Option<Value>,
|
body: Option<Value>,
|
||||||
|
headers: Option<Value>,
|
||||||
raw: bool,
|
raw: bool,
|
||||||
insecure: Option<bool>,
|
insecure: Option<bool>,
|
||||||
user: Option<String>,
|
user: Option<String>,
|
||||||
|
@ -109,6 +122,7 @@ fn run_post(
|
||||||
let args = Arguments {
|
let args = Arguments {
|
||||||
path: Some(call.req(engine_state, stack, 0)?),
|
path: Some(call.req(engine_state, stack, 0)?),
|
||||||
body: Some(call.req(engine_state, stack, 1)?),
|
body: Some(call.req(engine_state, stack, 1)?),
|
||||||
|
headers: call.get_flag(engine_state, stack, "headers")?,
|
||||||
raw: call.has_flag("raw"),
|
raw: call.has_flag("raw"),
|
||||||
user: call.get_flag(engine_state, stack, "user")?,
|
user: call.get_flag(engine_state, stack, "user")?,
|
||||||
password: call.get_flag(engine_state, stack, "password")?,
|
password: call.get_flag(engine_state, stack, "password")?,
|
||||||
|
@ -156,6 +170,7 @@ fn helper(
|
||||||
};
|
};
|
||||||
let user = args.user.clone();
|
let user = args.user.clone();
|
||||||
let password = args.password;
|
let password = args.password;
|
||||||
|
let headers = args.headers;
|
||||||
let location = url;
|
let location = url;
|
||||||
let raw = args.raw;
|
let raw = args.raw;
|
||||||
let login = match (user, password) {
|
let login = match (user, password) {
|
||||||
|
@ -185,6 +200,55 @@ fn helper(
|
||||||
if let Some(login) = login {
|
if let Some(login) = login {
|
||||||
request = request.header("Authorization", format!("Basic {}", login));
|
request = request.header("Authorization", format!("Basic {}", login));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some(headers) = headers {
|
||||||
|
let mut custom_headers: HashMap<String, Value> = HashMap::new();
|
||||||
|
|
||||||
|
match &headers {
|
||||||
|
Value::List { vals: table, .. } => {
|
||||||
|
if table.len() == 1 {
|
||||||
|
// single row([key1 key2]; [val1 val2])
|
||||||
|
match &table[0] {
|
||||||
|
Value::Record { cols, vals, .. } => {
|
||||||
|
for (k, v) in cols.iter().zip(vals.iter()) {
|
||||||
|
custom_headers.insert(k.to_string(), v.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
x => {
|
||||||
|
return Err(ShellError::CantConvert(
|
||||||
|
"string list or single row".into(),
|
||||||
|
x.get_type().to_string(),
|
||||||
|
headers.span().unwrap_or_else(|_| Span::new(0, 0)),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// primitive values ([key1 val1 key2 val2])
|
||||||
|
for row in table.chunks(2) {
|
||||||
|
if row.len() == 2 {
|
||||||
|
custom_headers.insert(row[0].as_string()?, (&row[1]).clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
x => {
|
||||||
|
return Err(ShellError::CantConvert(
|
||||||
|
"string list or single row".into(),
|
||||||
|
x.get_type().to_string(),
|
||||||
|
headers.span().unwrap_or_else(|_| Span::new(0, 0)),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
for (k, v) in &custom_headers {
|
||||||
|
if let Ok(s) = v.as_string() {
|
||||||
|
request = request.header(k, s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
match request.send() {
|
match request.send() {
|
||||||
Ok(resp) => match resp.headers().get("content-type") {
|
Ok(resp) => match resp.headers().get("content-type") {
|
||||||
Some(content_type) => {
|
Some(content_type) => {
|
||||||
|
|
Loading…
Reference in a new issue