Skip to content
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
14 changes: 10 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ jobs:
- "feat.: gzip"
- "feat.: brotli"
- "feat.: deflate"
- "feat.: query"
- "feat.: form"
- "feat.: json"
- "feat.: multipart"
- "feat.: stream"
Expand All @@ -102,23 +104,23 @@ jobs:
- name: windows / stable-x86_64-msvc
os: windows-latest
target: x86_64-pc-windows-msvc
features: "--features blocking,gzip,brotli,zstd,deflate,json,multipart,stream"
features: "--features blocking,gzip,brotli,zstd,deflate,query,form,json,multipart,stream"
- name: windows / stable-i686-msvc
os: windows-latest
target: i686-pc-windows-msvc
features: "--features blocking,gzip,brotli,zstd,deflate,json,multipart,stream"
features: "--features blocking,gzip,brotli,zstd,deflate,query,form,json,multipart,stream"
- name: windows / stable-x86_64-gnu
os: windows-latest
rust: stable-x86_64-pc-windows-gnu
target: x86_64-pc-windows-gnu
features: "--features blocking,gzip,brotli,zstd,deflate,json,multipart,stream"
features: "--features blocking,gzip,brotli,zstd,deflate,query,form,json,multipart,stream"
package_name: mingw-w64-x86_64-gcc
mingw64_path: "C:\\msys64\\mingw64\\bin"
- name: windows / stable-i686-gnu
os: windows-latest
rust: stable-i686-pc-windows-gnu
target: i686-pc-windows-gnu
features: "--features blocking,gzip,brotli,zstd,deflate,json,multipart,stream"
features: "--features blocking,gzip,brotli,zstd,deflate,query,form,json,multipart,stream"
package_name: mingw-w64-i686-gcc
mingw64_path: "C:\\msys64\\mingw32\\bin"

Expand Down Expand Up @@ -152,6 +154,10 @@ jobs:
features: "--features zstd,stream"
- name: "feat.: deflate"
features: "--features deflate,stream"
- name: "feat.: query"
features: "--features query"
- name: "feat.: form"
features: "--features form"
- name: "feat.: json"
features: "--features json"
- name: "feat.: multipart"
Expand Down
13 changes: 9 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ targets = ["x86_64-unknown-linux-gnu", "wasm32-unknown-unknown"]
features = [
"blocking",
"cookies",
"query",
"form",
"json",
"multipart",
]
Expand Down Expand Up @@ -74,7 +76,9 @@ zstd = ["tower-http/decompression-zstd"]

deflate = ["tower-http/decompression-deflate"]

json = ["dep:serde_json"]
query = ["dep:serde", "dep:serde_urlencoded"]
form = ["dep:serde", "dep:serde_urlencoded"]
json = ["dep:serde", "dep:serde_json"]

multipart = ["dep:mime_guess", "dep:futures-util"]

Expand Down Expand Up @@ -112,14 +116,15 @@ base64 = "0.22"
http = "1.1"
url = "2.4"
bytes = "1.2"
serde = "1.0"
serde_urlencoded = "0.7.1"
futures-core = { version = "0.3.28", default-features = false }
futures-util = { version = "0.3.28", default-features = false, optional = true }
sync_wrapper = { version = "1.0", features = ["futures"] }

# Optional deps...

serde = { version = "1.0", optional = true }
## query/form
serde_urlencoded = { version = "0.7.1", optional = true }
## json
serde_json = { version = "1.0", optional = true }
## multipart
Expand Down Expand Up @@ -189,7 +194,6 @@ futures-util = { version = "0.3.28", default-features = false, features = ["std"

[target.'cfg(target_arch = "wasm32")'.dependencies]
js-sys = "0.3.77"
serde_json = "1.0"
wasm-bindgen = "0.2.89"
wasm-bindgen-futures = "0.4.18"
wasm-streams = { version = "0.4", optional = true }
Expand Down Expand Up @@ -250,6 +254,7 @@ required-features = ["socks"]
[[example]]
name = "form"
path = "examples/form.rs"
required-features = ["form"]

[[example]]
name = "simple"
Expand Down
28 changes: 23 additions & 5 deletions src/async_impl/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::fmt;
use std::future::Future;
use std::time::Duration;

#[cfg(any(feature = "query", feature = "form", feature = "json"))]
use serde::Serialize;
#[cfg(feature = "json")]
use serde_json;
Expand All @@ -15,7 +16,9 @@ use super::response::Response;
use crate::config::{RequestConfig, TotalTimeout};
#[cfg(feature = "multipart")]
use crate::header::CONTENT_LENGTH;
use crate::header::{HeaderMap, HeaderName, HeaderValue, CONTENT_TYPE};
#[cfg(any(feature = "multipart", feature = "form", feature = "json"))]
use crate::header::CONTENT_TYPE;
use crate::header::{HeaderMap, HeaderName, HeaderValue};
use crate::{Method, Url};
use http::{request::Parts, Extensions, Request as HttpRequest, Version};

Expand Down Expand Up @@ -351,9 +354,15 @@ impl RequestBuilder {
/// as `.query(&[("key", "val")])`. It's also possible to serialize structs
/// and maps into a key-value pair.
///
/// # Optional
///
/// This requires the optional `query` feature to be enabled.
///
/// # Errors
/// This method will fail if the object you provide cannot be serialized
/// into a query string.
#[cfg(feature = "query")]
#[cfg_attr(docsrs, doc(cfg(feature = "query")))]
pub fn query<T: Serialize + ?Sized>(mut self, query: &T) -> RequestBuilder {
let mut error = None;
if let Ok(ref mut req) = self.request {
Expand Down Expand Up @@ -407,10 +416,16 @@ impl RequestBuilder {
/// # }
/// ```
///
/// # Optional
///
/// This requires the optional `form` feature to be enabled.
///
/// # Errors
///
/// This method fails if the passed value cannot be serialized into
/// url encoded format
#[cfg(feature = "form")]
#[cfg_attr(docsrs, doc(cfg(feature = "form")))]
pub fn form<T: Serialize + ?Sized>(mut self, form: &T) -> RequestBuilder {
let mut error = None;
if let Ok(ref mut req) = self.request {
Expand Down Expand Up @@ -663,13 +678,12 @@ impl TryFrom<Request> for HttpRequest<Body> {
mod tests {
#![cfg(not(feature = "rustls-tls-manual-roots-no-provider"))]

use super::{Client, HttpRequest, Request, RequestBuilder, Version};
use crate::Method;
use serde::Serialize;
use super::*;
#[cfg(feature = "query")]
use std::collections::BTreeMap;
use std::convert::TryFrom;

#[test]
#[cfg(feature = "query")]
fn add_query_append() {
let client = Client::new();
let some_url = "https://google.com/";
Expand All @@ -683,6 +697,7 @@ mod tests {
}

#[test]
#[cfg(feature = "query")]
fn add_query_append_same() {
let client = Client::new();
let some_url = "https://google.com/";
Expand All @@ -695,6 +710,7 @@ mod tests {
}

#[test]
#[cfg(feature = "query")]
fn add_query_struct() {
#[derive(Serialize)]
struct Params {
Expand All @@ -718,6 +734,7 @@ mod tests {
}

#[test]
#[cfg(feature = "query")]
fn add_query_map() {
let mut params = BTreeMap::new();
params.insert("foo", "bar");
Expand Down Expand Up @@ -759,6 +776,7 @@ mod tests {
}

#[test]
#[cfg(feature = "query")]
fn normalize_empty_query() {
let client = Client::new();
let some_url = "https://google.com/";
Expand Down
40 changes: 28 additions & 12 deletions src/blocking/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ use std::fmt;
use std::time::Duration;

use http::{request::Parts, Request as HttpRequest, Version};
#[cfg(any(feature = "query", feature = "form", feature = "json"))]
use serde::Serialize;
#[cfg(feature = "json")]
use serde_json;
use serde_urlencoded;

use super::body::{self, Body};
#[cfg(feature = "multipart")]
use super::multipart;
use super::Client;
use crate::header::{HeaderMap, HeaderName, HeaderValue, CONTENT_TYPE};
#[cfg(any(feature = "multipart", feature = "form", feature = "json"))]
use crate::header::CONTENT_TYPE;
use crate::header::{HeaderMap, HeaderName, HeaderValue};
use crate::{async_impl, Method, Url};

/// A request which can be executed with `Client::execute()`.
Expand Down Expand Up @@ -391,9 +393,15 @@ impl RequestBuilder {
/// as `.query(&[("key", "val")])`. It's also possible to serialize structs
/// and maps into a key-value pair.
///
/// # Optional
///
/// This requires the optional `query` feature to be enabled.
///
/// # Errors
/// This method will fail if the object you provide cannot be serialized
/// into a query string.
#[cfg(feature = "query")]
#[cfg_attr(docsrs, doc(cfg(feature = "query")))]
pub fn query<T: Serialize + ?Sized>(mut self, query: &T) -> RequestBuilder {
let mut error = None;
if let Ok(ref mut req) = self.request {
Expand Down Expand Up @@ -446,10 +454,16 @@ impl RequestBuilder {
/// # }
/// ```
///
/// # Optional
///
/// This requires the optional `form` feature to be enabled.
///
/// # Errors
///
/// This method fails if the passed value cannot be serialized into
/// url encoded format
#[cfg(feature = "form")]
#[cfg_attr(docsrs, doc(cfg(feature = "form")))]
pub fn form<T: Serialize + ?Sized>(mut self, form: &T) -> RequestBuilder {
let mut error = None;
if let Ok(ref mut req) = self.request {
Expand Down Expand Up @@ -681,16 +695,12 @@ fn fmt_request_fields<'a, 'b>(

#[cfg(test)]
mod tests {
use super::super::{body, Client};
use super::{HttpRequest, Request, Version};
use crate::header::{HeaderMap, HeaderValue, ACCEPT, CONTENT_TYPE, HOST};
use crate::Method;
use serde::Serialize;
#[cfg(feature = "json")]
use serde_json;
use serde_urlencoded;
use std::collections::{BTreeMap, HashMap};
use std::time::Duration;
use super::*;
use crate::header::{ACCEPT, HOST};
#[cfg(feature = "query")]
use std::collections::BTreeMap;
#[cfg(feature = "form")]
use std::collections::HashMap;

#[test]
fn basic_get_request() {
Expand Down Expand Up @@ -825,6 +835,7 @@ mod tests {
}

#[test]
#[cfg(feature = "query")]
fn add_query_append() {
let client = Client::new();
let some_url = "https://google.com/";
Expand All @@ -838,6 +849,7 @@ mod tests {
}

#[test]
#[cfg(feature = "query")]
fn add_query_append_same() {
let client = Client::new();
let some_url = "https://google.com/";
Expand All @@ -850,6 +862,7 @@ mod tests {
}

#[test]
#[cfg(feature = "query")]
fn add_query_struct() {
#[derive(Serialize)]
struct Params {
Expand All @@ -873,6 +886,7 @@ mod tests {
}

#[test]
#[cfg(feature = "query")]
fn add_query_map() {
let mut params = BTreeMap::new();
params.insert("foo", "bar");
Expand All @@ -889,6 +903,7 @@ mod tests {
}

#[test]
#[cfg(feature = "form")]
fn add_form() {
let client = Client::new();
let some_url = "https://google.com/";
Expand Down Expand Up @@ -983,6 +998,7 @@ mod tests {
}

#[test]
#[cfg(feature = "query")]
fn normalize_empty_query() {
let client = Client::new();
let some_url = "https://google.com/";
Expand Down
9 changes: 8 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,12 @@
//! This can be an array of tuples, or a `HashMap`, or a custom type that
//! implements [`Serialize`][serde].
//!
//! The feature `form` is required.
//!
//! ```rust
//! # use reqwest::Error;
//! #
//! # #[cfg(feature = "form")]
//! # async fn run() -> Result<(), Error> {
//! // This will POST a body of `foo=bar&baz=quux`
//! let params = [("foo", "bar"), ("baz", "quux")];
Expand All @@ -101,7 +104,9 @@
//!
//! There is also a `json` method helper on the [`RequestBuilder`][builder] that works in
//! a similar fashion the `form` method. It can take any value that can be
//! serialized into JSON. The feature `json` is required.
//! serialized into JSON.
//!
//! The feature `json` is required.
//!
//! ```rust
//! # use reqwest::Error;
Expand Down Expand Up @@ -205,6 +210,8 @@
//! - **brotli**: Provides response body brotli decompression.
//! - **zstd**: Provides response body zstd decompression.
//! - **deflate**: Provides response body deflate decompression.
//! - **query**: Provides query parameter serialization.
//! - **form**: Provides form data serialization.
//! - **json**: Provides serialization and deserialization for JSON bodies.
//! - **multipart**: Provides functionality for multipart forms.
//! - **stream**: Adds support for `futures::Stream`.
Expand Down
Loading