[Ruby to Rust] Format String to JSON and Curl

As the title says, I am trying to format a string to JSON and curl it to a slack channel. In ruby:

body = {"text" => message}.to_json
`curl -XPOST #{slackchannel} -d 'payload=#{body}'`
puts body

I'm unsure how to convert the string to JSON, but I tried curling it using:

use reqwest::blocking::Client;
let response = Client::post(slackchannel)
        .body(format!("payload={}", body))

But I get the error " use of undeclared crate or module reqwest". I couldn't find something among the lines of "extern crate reqwest" in the reqwest documentation. I did add the dependencies: "reqwest = { version = "0.11", features = ["blocking"]}"
Any help would be appreciated.

Nevermind, I fixed it by adding "json" to the dependency: "reqwest = { version = "0.11", features = ["blocking", "json"]}". I am still unsure about formatting a string as json.
I have also tried:

let body = format!("payload={}", message);
    let client = reqwest::Client::new();
    let response = client.post(slackchan)

But I get the error:

15  |   fn main() {
    |      ---- this is not `async`
241 |       let response = client.post(slackchan)
    |  ____________________^
242 | |         .json(&body)
243 | |         .send()
244 | |         .await();
    | |______________^ only allowed inside `async` functions and blocks

For simple programs that don't need async/await, use request::blocking::Client instead of request::Client.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.