You can use the reqwest
library in combination with serde_json
to easily make a HTTP request with a custom JSON body.
First, add the following dependencies to your Cargo.toml
file:
88 chars4 lines
Then, you can use the following code snippet to make a POST request with a custom JSON body:
main.rs433 chars21 lines
In this example, we define a struct CustomBody
using serde
's Serialize
trait to allow it to easily be converted to JSON. We then create a new instance of CustomBody
and pass it to the json
method on the POST
request, which will automatically convert it to a JSON body. Finally, we send the request using await
(in an async context).
Note that the URL used in the example is only for demonstration purposes and will not actually work.
gistlibby LogSnag