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 body_form method to RequestBuilder #330

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
31 changes: 31 additions & 0 deletions src/request_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,37 @@ impl RequestBuilder {
Ok(self.body(Body::from_json(json)?))
}

/// Pass form data as the request body.
///
/// # Mime
///
/// The encoding is set to `application/x-www-form-urlencoded`.
///
/// # Errors
///
/// This method will return an error if the provided data could not be serialized to urlencoded string.
///
/// # Examples
///
/// ```no_run
/// # use serde::{Deserialize, Serialize};
/// # #[async_std::main]
/// # async fn main() -> surf::Result<()> {
/// #[derive(Deserialize, Serialize)]
/// struct Ip {
/// ip: String
/// }
///
/// let uri = "https://httpbin.org/post";
/// let data = &Ip { ip: "129.0.0.1".into() };
/// let res = surf::post(uri).body_form(data)?.await?;
/// assert_eq!(res.status(), 200);
/// # Ok(()) }
/// ```
pub fn body_form(self, form: &impl Serialize) -> crate::Result<Self> {
Ok(self.body(Body::from_form(form)?))
}

/// Pass a string as the request body.
///
/// # Mime
Expand Down