Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for multipart/form-data bodies #258

Merged
merged 2 commits into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
- Structured bodies can now be defined with tags on the `body` field of a recipe, making it more convenient to construct bodies of common types. Supported types are:
- `!json` [#242](https://github.com/LucasPickering/slumber/issues/242)
- `!form_urlencoded` [#244](https://github.com/LucasPickering/slumber/issues/244)
- `!form_multipart` [#243](https://github.com/LucasPickering/slumber/issues/243)
- [See docs](https://slumber.lucaspickering.me/book/api/request_collection/recipe_body.html) for usage instructions
- Support multiple instances of the same query param [#245](https://github.com/LucasPickering/slumber/issues/245) (@maksimowiczm)
- Query params can now be defined as a list of `<param>=<value>` entries
Expand Down
25 changes: 23 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ nom = "7.1.3"
notify = {version = "^6.1.1", default-features = false, features = ["macos_fsevent"]}
open = "5.1.1"
ratatui = {version = "^0.26.0", features = ["serde", "unstable-rendered-line-info"]}
reqwest = {version = "^0.12.4", default-features = false, features = ["rustls-tls"]}
reqwest = {version = "^0.12.4", default-features = false, features = ["multipart", "rustls-tls"]}
rmp-serde = "^1.1.2"
rusqlite = {version = "^0.31.0", default-features = false, features = ["bundled", "chrono", "uuid"]}
rusqlite_migration = "^1.2.0"
Expand All @@ -52,6 +52,7 @@ uuid = {version = "^1.4.1", default-features = false, features = ["serde", "v4"]
[dev-dependencies]
mockito = {version = "1.4.0", default-features = false}
pretty_assertions = "1.4.0"
regex = {version = "1.10.5", default-features = false}
rstest = {version = "0.19.0", default-features = false}
serde_test = "1.0.176"

Expand Down
24 changes: 20 additions & 4 deletions docs/src/api/request_collection/recipe_body.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ In addition, you can pass any [`Template`](./template.md) to render any text or

The following content types have first-class support. Slumber will automatically set the `Content-Type` header to the specified value, but you can override this simply by providing your own value for the header.

| Variant | Type | `Content-Type` | Description |
| ------------------ | -------------------------------------------- | ----------------------------------- | --------------------------------------------------------------------------------------------------------- |
| `!json` | Any | `application/json` | Structured JSON body; all strings are treated as templates |
| `!form_urlencoded` | [`mapping[string, Template]`](./template.md) | `application/x-www-form-urlencoded` | URL-encoded form data; [see here for more](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST |
| Variant | Type | `Content-Type` | Description |
| ------------------ | -------------------------------------------- | ----------------------------------- | ---------------------------------------------------------------------------------------------------------- |
| `!json` | Any | `application/json` | Structured JSON body; all strings are treated as templates |
| `!form_urlencoded` | [`mapping[string, Template]`](./template.md) | `application/x-www-form-urlencoded` | URL-encoded form data; [see here for more](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST) |
| `!form_multipart` | [`mapping[string, Template]`](./template.md) | `multipart/form-data` | Binary form data; [see here for more](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST) |

## Examples

Expand Down Expand Up @@ -41,4 +42,19 @@ requests:
url: "{{host}}/fishes/{{fish_id}}"
# Content-Type header will be set automatically based on the body type
body: !json { "name": "Alfonso" }

urlencoded_body: !request
method: POST
url: "{{host}}/fishes/{{fish_id}}"
# Content-Type header will be set automatically based on the body type
body: !form_urlencoded
name: Alfonso

multipart_body: !request
method: POST
url: "{{host}}/fishes/{{fish_id}}"
# Content-Type header will be set automatically based on the body type
body: !form_multipart
name: Alfonso
image: "{{chains.fish_image}}"
```
11 changes: 11 additions & 0 deletions slumber.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ chains:
recipe: login
trigger: !expire 12h
selector: $.data
image:
source: !file
path: ./static/slumber.png

.ignore:
base: &base
Expand Down Expand Up @@ -81,6 +84,14 @@ requests:
method: GET
url: "{{host}}/image"

upload_image: !request
name: Upload Image
method: POST
url: "{{host}}/anything/image"
body: !form_multipart
filename: "logo.png"
image: "{{chains.image}}"

delay: !request
<<: *base
name: Delay
Expand Down
5 changes: 3 additions & 2 deletions src/cli/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
GlobalArgs,
};
use anyhow::anyhow;
use bytesize::ByteSize;
use clap::Parser;
use dialoguer::console::Style;
use std::process::ExitCode;
Expand Down Expand Up @@ -94,8 +95,8 @@ impl HistoryCommand {
print!(
"{} ({})\n{}",
subheader_style.apply_to("Body"),
body.size(),
MaybeStr(body.bytes())
ByteSize(body.len() as u64),
MaybeStr(body)
)
}
println!();
Expand Down
21 changes: 18 additions & 3 deletions src/collection/cereal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,12 @@ impl RecipeBody {
const STRUCT_NAME: &'static str = "RecipeBody";
const VARIANT_JSON: &'static str = "json";
const VARIANT_FORM_URLENCODED: &'static str = "form_urlencoded";
const ALL_VARIANTS: &'static [&'static str] =
&[Self::VARIANT_JSON, Self::VARIANT_FORM_URLENCODED];
const VARIANT_FORM_MULTIPART: &'static str = "form_multipart";
const ALL_VARIANTS: &'static [&'static str] = &[
Self::VARIANT_JSON,
Self::VARIANT_FORM_URLENCODED,
Self::VARIANT_FORM_MULTIPART,
];
}

/// Custom serialization for RecipeBody, so the `Raw` variant serializes as a
Expand All @@ -255,6 +259,13 @@ impl Serialize for RecipeBody {
Self::VARIANT_FORM_URLENCODED,
value,
),
RecipeBody::FormMultipart(value) => serializer
.serialize_newtype_variant(
Self::STRUCT_NAME,
3,
Self::VARIANT_FORM_MULTIPART,
value,
),
}
}
}
Expand Down Expand Up @@ -316,6 +327,9 @@ impl<'de> Deserialize<'de> for RecipeBody {
RecipeBody::VARIANT_FORM_URLENCODED => {
Ok(RecipeBody::FormUrlencoded(value.newtype_variant()?))
}
RecipeBody::VARIANT_FORM_MULTIPART => {
Ok(RecipeBody::FormMultipart(value.newtype_variant()?))
}
other => Err(A::Error::unknown_variant(
other,
RecipeBody::ALL_VARIANTS,
Expand Down Expand Up @@ -519,7 +533,8 @@ mod tests {
tag: Tag::new("raw"),
value: "{{user_id}}".into()
})),
"unknown variant `raw`, expected `json` or `form_urlencoded`",
"unknown variant `raw`, expected one of \
`json`, `form_urlencoded`, `form_multipart`",
)]
#[case::form_urlencoded_wrong_type(
serde_yaml::Value::Tagged(Box::new(TaggedValue{
Expand Down
Loading
Loading