Send a query, headers and a form
For Rust developers calling a web API that takes query parameters, headers or form posts
You add query parameters and headers to a GET request and send a form in a POST. httpbin.org echoes back what it received, so you can see exactly what went over the wire.
Before you start
You need Rust with Cargo. fibre_http comes from crates.io:
fibre_http = "0"Set up the project#
Start a new binary crate.
Put this in the [dependencies] section of Cargo.toml:
[]
= { = "0", = ["json"] }
= "1"The json feature lets you read httpbin's answer as JSON. Sending a query, headers and a form does not need it.
The program#
Replace src/main.rs with this:
use SyncClient;
use Value;
Run it:
What each part does#
query takes a list of name and value pairs. It encodes them for you, which is why the space in rust http became a + in the URL. If the URL already has a query, the new pairs go after it.
header adds one header. Call it again for each header you need.
form encodes the pairs as a form body and sets Content-Type to application/x-www-form-urlencoded. If you set your own Content-Type header first, form leaves it alone.
json() reads the body and parses it. Here it parses into serde_json::Value, so you can pick fields out by name.
Where to go next#
The next tutorial sends and receives your own Rust types as JSON. The usage guide covers raw bodies and Expect: 100-continue: https://github.com/excsn/fibre/blob/main/http/README.USAGE.md
