Skip to main content

post

Function post 

Source
pub fn post(
    url: &str,
    content_type: &str,
    body: &[u8],
) -> Result<ClientResponse, Error>
Available on crate feature std only.
Expand description

Sends body to url with POST and content-type: content_type, using the default Client.

§Errors

As get, plus Error::Http for a content type that is not a valid field value.

§Examples

use http_net::{blocking, Response};

let server = blocking::Server::bind("127.0.0.1:0")?;
let addr = server.local_addr();
let stop = server.shutdown_handle();
let t = std::thread::spawn(move || server.serve(|_, body| Response::text(200, &body.len().to_string())));

let resp = blocking::post(&format!("http://{addr}/notes"), "text/plain", b"hello")?;
assert_eq!(resp.text(), Some("5"));

stop.shutdown();
t.join().unwrap()?;