|
| 1 | +--- |
| 2 | +title: Post Request Guides |
| 3 | +layout: guide |
| 4 | +--- |
| 5 | + |
| 6 | +## POST |
| 7 | +Now that we've seen how to make a GET request, let's look at how to make a POST request. This is useful when you need to send data to the server, such as when submitting a form or uploading a file. |
| 8 | + |
| 9 | +To make a POST request, we'll need to change a few things from our GET request: |
| 10 | + |
| 11 | +1. We'll set the method to POST. |
| 12 | +2. We'll need to provide a request body. |
| 13 | +3. We'll need to specify the type of data in our body by adding a `hyper::header::CONTENT_TYPE` header. |
| 14 | + |
| 15 | +For the body, we have a couple of options. We can use a simple string, a JSON string, or we can use raw bytes. Let's look at all three: |
| 16 | + |
| 17 | +```rust |
| 18 | +# extern crate http_body_util; |
| 19 | +# extern crate hyper; |
| 20 | +# extern crate hyper_util; |
| 21 | +# extern crate tokio; |
| 22 | +# use http_body_util::Empty; |
| 23 | +# use hyper::body::Bytes; |
| 24 | +# use hyper::Request; |
| 25 | +# use hyper_util::rt::TokioIo; |
| 26 | +# use tokio::net::TcpStream; |
| 27 | +# async fn run() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { |
| 28 | +# let url = "http://httpbin.org/ip".parse::<hyper::Uri>()?; |
| 29 | +# let host = url.host().expect("uri has no host"); |
| 30 | +# let port = url.port_u16().unwrap_or(80); |
| 31 | +# let addr = format!("{}:{}", host, port); |
| 32 | +# let stream = TcpStream::connect(addr).await?; |
| 33 | +# let io = TokioIo::new(stream); |
| 34 | +# let (mut sender, conn) = hyper::client::conn::http1::handshake(io).await?; |
| 35 | +# tokio::task::spawn(async move { |
| 36 | +# if let Err(err) = conn.await { |
| 37 | +# println!("Connection failed: {:?}", err); |
| 38 | +# } |
| 39 | +# }); |
| 40 | + |
| 41 | +// The authority of our URL will be the hostname of the httpbin remote |
| 42 | +let authority = url.authority().unwrap().clone(); |
| 43 | + |
| 44 | +// For plain text |
| 45 | +let req_body = Full::<Bytes>::from("Some plain text as a body."); |
| 46 | +let req = Request::builder() |
| 47 | + .method(hyper::Method::POST) |
| 48 | + .header(hyper::header::HOST, authority.as_str()) |
| 49 | + .header(hyper::header::CONTENT_TYPE, "text/plain") |
| 50 | + .body(req_body)?; |
| 51 | + |
| 52 | +// For JSON data |
| 53 | +let json_data = r#"{"key": "value"}"#; |
| 54 | +let req_body = Full::<Bytes>::from(json_data); |
| 55 | +let req = Request::builder() |
| 56 | + .method(hyper::Method::POST) |
| 57 | + .header(hyper::header::HOST, authority.as_str()) |
| 58 | + .header(hyper::header::CONTENT_TYPE, "application/json") |
| 59 | + .body(req_body)?; |
| 60 | + |
| 61 | +// For binary data |
| 62 | +let binary_data = vec![0u8; 128]; // Example binary data |
| 63 | +let req_body = Full::<Bytes>::from(binary_data); |
| 64 | +let req = Request::builder() |
| 65 | + .method(hyper::Method::POST) |
| 66 | + .header(hyper::header::HOST, authority.as_str()) |
| 67 | + .header(hyper::header::CONTENT_TYPE, "application/octet-stream") |
| 68 | + .body(req_body)?; |
| 69 | + |
| 70 | + |
| 71 | +let res = sender.send_request(req).await?; |
| 72 | + |
| 73 | +println!("Response status: {}", res.status()); |
| 74 | +# Ok(()) |
| 75 | +# } |
| 76 | +# fn main() {} |
| 77 | +``` |
0 commit comments