to text
sets content type to text/plain. Http client will now set content type when BodyType::Unknown if it there is one
This commit is contained in:
parent
8c93fb7f2d
commit
a5a804e336
|
@ -1,6 +1,8 @@
|
||||||
use chrono_humanize::HumanTime;
|
use chrono_humanize::HumanTime;
|
||||||
use nu_engine::command_prelude::*;
|
use nu_engine::command_prelude::*;
|
||||||
use nu_protocol::{format_duration, format_filesize_from_conf, ByteStream, Config};
|
use nu_protocol::{
|
||||||
|
format_duration, format_filesize_from_conf, ByteStream, Config, PipelineMetadata,
|
||||||
|
};
|
||||||
|
|
||||||
const LINE_ENDING: &str = if cfg!(target_os = "windows") {
|
const LINE_ENDING: &str = if cfg!(target_os = "windows") {
|
||||||
"\r\n"
|
"\r\n"
|
||||||
|
@ -37,10 +39,14 @@ impl Command for ToText {
|
||||||
let input = input.try_expand_range()?;
|
let input = input.try_expand_range()?;
|
||||||
|
|
||||||
match input {
|
match input {
|
||||||
PipelineData::Empty => Ok(Value::string(String::new(), span).into_pipeline_data()),
|
PipelineData::Empty => Ok(Value::string(String::new(), span)
|
||||||
|
.into_pipeline_data_with_metadata(update_metadata(None))),
|
||||||
PipelineData::Value(value, ..) => {
|
PipelineData::Value(value, ..) => {
|
||||||
let str = local_into_string(value, LINE_ENDING, engine_state.get_config());
|
let str = local_into_string(value, LINE_ENDING, engine_state.get_config());
|
||||||
Ok(Value::string(str, span).into_pipeline_data())
|
Ok(
|
||||||
|
Value::string(str, span)
|
||||||
|
.into_pipeline_data_with_metadata(update_metadata(None)),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
PipelineData::ListStream(stream, meta) => {
|
PipelineData::ListStream(stream, meta) => {
|
||||||
let span = stream.span();
|
let span = stream.span();
|
||||||
|
@ -57,10 +63,12 @@ impl Command for ToText {
|
||||||
engine_state.ctrlc.clone(),
|
engine_state.ctrlc.clone(),
|
||||||
ByteStreamType::String,
|
ByteStreamType::String,
|
||||||
),
|
),
|
||||||
meta,
|
update_metadata(meta),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
PipelineData::ByteStream(stream, meta) => Ok(PipelineData::ByteStream(stream, meta)),
|
PipelineData::ByteStream(stream, meta) => {
|
||||||
|
Ok(PipelineData::ByteStream(stream, update_metadata(meta)))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -124,6 +132,14 @@ fn local_into_string(value: Value, separator: &str, config: &Config) -> String {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn update_metadata(metadata: Option<PipelineMetadata>) -> Option<PipelineMetadata> {
|
||||||
|
metadata
|
||||||
|
.map(|md| md.with_content_type(Some("text/plain".to_string())))
|
||||||
|
.or_else(|| {
|
||||||
|
Some(PipelineMetadata::default().with_content_type(Some("text/plain".to_string())))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
@ -225,37 +225,34 @@ pub fn send_request2(
|
||||||
send_cancellable_request_bytes(&request_url, req, byte_stream, ctrl_c)
|
send_cancellable_request_bytes(&request_url, req, byte_stream, ctrl_c)
|
||||||
}
|
}
|
||||||
HttpBody::Value(body) => {
|
HttpBody::Value(body) => {
|
||||||
let body_type = match content_type {
|
let (body_type, req) = match content_type {
|
||||||
Some(it) if it == "application/json" => BodyType::Json,
|
Some(it) if it == "application/json" => (BodyType::Json, request),
|
||||||
Some(it) if it == "application/x-www-form-urlencoded" => BodyType::Form,
|
Some(it) if it == "application/x-www-form-urlencoded" => (BodyType::Form, request),
|
||||||
_ => BodyType::Unknown,
|
Some(it) => {
|
||||||
|
let r = request.clone().set("Content-Type", &it);
|
||||||
|
(BodyType::Unknown, r)
|
||||||
|
}
|
||||||
|
_ => (BodyType::Unknown, request),
|
||||||
};
|
};
|
||||||
|
|
||||||
match body {
|
match body {
|
||||||
Value::Binary { val, .. } => send_cancellable_request(
|
Value::Binary { val, .. } => send_cancellable_request(
|
||||||
&request_url,
|
&request_url,
|
||||||
Box::new(move || request.send_bytes(&val)),
|
Box::new(move || req.send_bytes(&val)),
|
||||||
ctrl_c,
|
ctrl_c,
|
||||||
),
|
),
|
||||||
Value::String { .. } if body_type == BodyType::Json => {
|
Value::String { .. } if body_type == BodyType::Json => {
|
||||||
let data = value_to_json_value(&body)?;
|
let data = value_to_json_value(&body)?;
|
||||||
send_cancellable_request(
|
send_cancellable_request(&request_url, Box::new(|| req.send_json(data)), ctrl_c)
|
||||||
&request_url,
|
|
||||||
Box::new(|| request.send_json(data)),
|
|
||||||
ctrl_c,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
Value::String { val, .. } => send_cancellable_request(
|
Value::String { val, .. } => send_cancellable_request(
|
||||||
&request_url,
|
&request_url,
|
||||||
Box::new(move || request.send_string(&val)),
|
Box::new(move || req.send_string(&val)),
|
||||||
ctrl_c,
|
ctrl_c,
|
||||||
),
|
),
|
||||||
Value::Record { .. } if body_type == BodyType::Json => {
|
Value::Record { .. } if body_type == BodyType::Json => {
|
||||||
let data = value_to_json_value(&body)?;
|
let data = value_to_json_value(&body)?;
|
||||||
send_cancellable_request(
|
send_cancellable_request(&request_url, Box::new(|| req.send_json(data)), ctrl_c)
|
||||||
&request_url,
|
|
||||||
Box::new(|| request.send_json(data)),
|
|
||||||
ctrl_c,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
Value::Record { val, .. } if body_type == BodyType::Form => {
|
Value::Record { val, .. } if body_type == BodyType::Form => {
|
||||||
let mut data: Vec<(String, String)> = Vec::with_capacity(val.len());
|
let mut data: Vec<(String, String)> = Vec::with_capacity(val.len());
|
||||||
|
@ -270,7 +267,7 @@ pub fn send_request2(
|
||||||
.iter()
|
.iter()
|
||||||
.map(|(a, b)| (a.as_str(), b.as_str()))
|
.map(|(a, b)| (a.as_str(), b.as_str()))
|
||||||
.collect::<Vec<(&str, &str)>>();
|
.collect::<Vec<(&str, &str)>>();
|
||||||
request.send_form(&data)
|
req.send_form(&data)
|
||||||
};
|
};
|
||||||
send_cancellable_request(&request_url, Box::new(request_fn), ctrl_c)
|
send_cancellable_request(&request_url, Box::new(request_fn), ctrl_c)
|
||||||
}
|
}
|
||||||
|
@ -292,17 +289,13 @@ pub fn send_request2(
|
||||||
.iter()
|
.iter()
|
||||||
.map(|(a, b)| (a.as_str(), b.as_str()))
|
.map(|(a, b)| (a.as_str(), b.as_str()))
|
||||||
.collect::<Vec<(&str, &str)>>();
|
.collect::<Vec<(&str, &str)>>();
|
||||||
request.send_form(&data)
|
req.send_form(&data)
|
||||||
};
|
};
|
||||||
send_cancellable_request(&request_url, Box::new(request_fn), ctrl_c)
|
send_cancellable_request(&request_url, Box::new(request_fn), ctrl_c)
|
||||||
}
|
}
|
||||||
Value::List { .. } if body_type == BodyType::Json => {
|
Value::List { .. } if body_type == BodyType::Json => {
|
||||||
let data = value_to_json_value(&body)?;
|
let data = value_to_json_value(&body)?;
|
||||||
send_cancellable_request(
|
send_cancellable_request(&request_url, Box::new(|| req.send_json(data)), ctrl_c)
|
||||||
&request_url,
|
|
||||||
Box::new(|| request.send_json(data)),
|
|
||||||
ctrl_c,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
_ => Err(ShellErrorOrRequestError::ShellError(ShellError::IOError {
|
_ => Err(ShellErrorOrRequestError::ShellError(ShellError::IOError {
|
||||||
msg: "unsupported body input".into(),
|
msg: "unsupported body input".into(),
|
||||||
|
|
Loading…
Reference in New Issue
Block a user