mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 19:37:45 +02:00
Add multipart/form-data uploads (#13532)
Fixes nushell/nushell#11046 # Description This adds support for `multipart/form-data` (RFC 7578) uploads to nushell. Binary data is uploaded as files (`application/octet-stream`), everything else is uploaded as plain text. ```console $ http post https://echo.free.beeceptor.com --content-type multipart/form-data {cargo: (open -r Cargo.toml | into binary ), description: "It's some TOML"} | upsert ip "<redacted>" ╭───────────────────┬─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ method │ POST │ │ protocol │ https │ │ host │ echo.free.beeceptor.com │ │ path │ / │ │ ip │ <redacted> │ │ │ ╭─────────────────┬────────────────────────────────────────────────────────────────────╮ │ │ headers │ │ Host │ echo.free.beeceptor.com │ │ │ │ │ User-Agent │ nushell │ │ │ │ │ Content-Length │ 9453 │ │ │ │ │ Accept │ */* │ │ │ │ │ Accept-Encoding │ gzip │ │ │ │ │ Content-Type │ multipart/form-data; boundary=a15f6a14-5768-4a6a-b3a4-686a112d9e27 │ │ │ │ ╰─────────────────┴────────────────────────────────────────────────────────────────────╯ │ │ parsedQueryParams │ {record 0 fields} │ │ │ ╭─────────────────┬───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ │ parsedBody │ │ │ ╭─────────────┬────────────────╮ │ │ │ │ │ textFields │ │ description │ It's some TOML │ │ │ │ │ │ │ ╰─────────────┴────────────────╯ │ │ │ │ │ │ ╭───┬───────┬──────────┬──────────────────────────┬───────────────────────────┬───────────────────────────────────────────┬────────────────╮ │ │ │ │ │ files │ │ # │ name │ fileName │ Content-Type │ Content-Transfer-Encoding │ Content-Disposition │ Content-Length │ │ │ │ │ │ │ ├───┼───────┼──────────┼──────────────────────────┼───────────────────────────┼───────────────────────────────────────────┼────────────────┤ │ │ │ │ │ │ │ 0 │ cargo │ cargo │ application/octet-stream │ binary │ form-data; name="cargo"; filename="cargo" │ 9101 │ │ │ │ │ │ │ ╰───┴───────┴──────────┴──────────────────────────┴───────────────────────────┴───────────────────────────────────────────┴────────────────╯ │ │ │ │ ╰─────────────────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │ ╰───────────────────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ ``` # User-Facing Changes `http post --content-type multipart/form-data` now accepts a record which is uploaded as `multipart/form-data`. Binary data is uploaded as files (`application/octet-stream`), everything else is uploaded as plain text. Previously `http post --content-type multipart/form-data` rejected records, so there's no BC break. # Tests + Formatting Added. # After Submitting - [ ] update docs to showcase new functionality
This commit is contained in:
@ -4,10 +4,12 @@ use base64::{
|
||||
engine::{general_purpose::PAD, GeneralPurpose},
|
||||
Engine,
|
||||
};
|
||||
use multipart_rs::MultipartWriter;
|
||||
use nu_engine::command_prelude::*;
|
||||
use nu_protocol::{ByteStream, Signals};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
io::Cursor,
|
||||
path::PathBuf,
|
||||
str::FromStr,
|
||||
sync::mpsc::{self, RecvTimeoutError},
|
||||
@ -20,6 +22,7 @@ use url::Url;
|
||||
pub enum BodyType {
|
||||
Json,
|
||||
Form,
|
||||
Multipart,
|
||||
Unknown,
|
||||
}
|
||||
|
||||
@ -210,6 +213,7 @@ pub fn send_request(
|
||||
let (body_type, req) = match content_type {
|
||||
Some(it) if it == "application/json" => (BodyType::Json, request),
|
||||
Some(it) if it == "application/x-www-form-urlencoded" => (BodyType::Form, request),
|
||||
Some(it) if it == "multipart/form-data" => (BodyType::Multipart, request),
|
||||
Some(it) => {
|
||||
let r = request.clone().set("Content-Type", &it);
|
||||
(BodyType::Unknown, r)
|
||||
@ -265,6 +269,48 @@ pub fn send_request(
|
||||
};
|
||||
send_cancellable_request(&request_url, Box::new(request_fn), span, signals)
|
||||
}
|
||||
// multipart form upload
|
||||
Value::Record { val, .. } if body_type == BodyType::Multipart => {
|
||||
let mut builder = MultipartWriter::new();
|
||||
|
||||
let err = |e| {
|
||||
ShellErrorOrRequestError::ShellError(ShellError::IOError {
|
||||
msg: format!("failed to build multipart data: {}", e),
|
||||
})
|
||||
};
|
||||
|
||||
for (col, val) in val.into_owned() {
|
||||
if let Value::Binary { val, .. } = val {
|
||||
let headers = [
|
||||
"Content-Type: application/octet-stream".to_string(),
|
||||
"Content-Transfer-Encoding: binary".to_string(),
|
||||
format!(
|
||||
"Content-Disposition: form-data; name=\"{}\"; filename=\"{}\"",
|
||||
col, col
|
||||
),
|
||||
format!("Content-Length: {}", val.len()),
|
||||
];
|
||||
builder
|
||||
.add(&mut Cursor::new(val), &headers.join("\n"))
|
||||
.map_err(err)?;
|
||||
} else {
|
||||
let headers =
|
||||
format!(r#"Content-Disposition: form-data; name="{}""#, col);
|
||||
builder
|
||||
.add(val.coerce_into_string()?.as_bytes(), &headers)
|
||||
.map_err(err)?;
|
||||
}
|
||||
}
|
||||
builder.finish();
|
||||
|
||||
let (boundary, data) = (builder.boundary, builder.data);
|
||||
let content_type = format!("multipart/form-data; boundary={}", boundary);
|
||||
|
||||
let request_fn =
|
||||
move || req.set("Content-Type", &content_type).send_bytes(&data);
|
||||
|
||||
send_cancellable_request(&request_url, Box::new(request_fn), span, signals)
|
||||
}
|
||||
Value::List { vals, .. } if body_type == BodyType::Form => {
|
||||
if vals.len() % 2 != 0 {
|
||||
return Err(ShellErrorOrRequestError::ShellError(ShellError::IOError {
|
||||
|
@ -127,6 +127,11 @@ impl Command for SubCommand {
|
||||
example: "open foo.json | http post https://www.example.com",
|
||||
result: None,
|
||||
},
|
||||
Example {
|
||||
description: "Upload a file to example.com",
|
||||
example: "http post --content-type multipart/form-data https://www.example.com { audio: (open -r file.mp3) }",
|
||||
result: None,
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user