3

I am trying to make post request using reqwest crate of rust. Here is the snippet of cargo.toml

[dependencies]
tokio = { version = "0.2", features = ["full"] }
reqwest = { version = "0.10", features = ["json"] }

Here is the snippet of code from which I am making request to simple server.

use reqwest::{Response, StatusCode};
use serde_json::{json, Value};

#[tokio::main]
async fn main() ->  Result< (),  reqwest::Error>  {

    let map1 = json!({
        "abc":"abc",
        "efg":"efg"
    });

    let body: Value = reqwest::Client::new()
    .post("http://localhost:4000/hello")
    .json(&map1)
    .send()
    .await?
    .json()
    .await?;

    println!("Data is : {:?}", body);

    Ok(())
}

The snippet of the code written using simple server crate from which I am serving this request is below:

use simple_server::{Server, StatusCode};

fn main() {
    let server = Server::new(|req, mut response| {

        println!(
            "Request received {} {} {:?}",
            req.method(),
            req.uri(),
            &req.body()
        );

        match (req.method().as_str(), req.uri().path()) {
            ("GET", "/") => Ok(response.body(format!("Get request").into_bytes())?),

            ("POST", "/hello") => Ok(response.body(format!("Post request").into_bytes())?),

            (_, _) => {
                response.status(StatusCode::NOT_FOUND);
                Ok(response.body(String::from("Not Found").into_bytes())?)
            }
        }
    });

    server.listen("127.0.0.1", "4000");
}

The output I get is :

Request received POST /hello []

The desired output is the vector array of bytes but I am receiving a empty vector array.

The solutions which I have already tried are:

  • Making a post request using Postman to the same server and it is working fine.
  • Making a post request using the same reqwest code to any other server such as hyper, actix, etc and it is working fine.
  • Sending a simple body as a body of post request (no JSON ). But the same issue occurs.

So I think the issue must be with this simple server crate. Every worthy suggestion will be Encouraged.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Jawwad Turabi
  • 322
  • 4
  • 12
  • 1
    I tried making several requests in a row (using `wget --post-file=...`) and it appears to randomly show the data or an empty result. Sounds like a bug in `simple-server` – Jmb Apr 20 '20 at 13:52
  • @Jmb, you're right. I am having the same, it works sometimes. Most probably a bug. – Jawwad Turabi Apr 20 '20 at 14:37

1 Answers1

0
use reqwest::{Response, StatusCode};
use serde_json::{json, Value};

#[tokio::main]
async fn main() ->  Result<String>  {

 let map1 = json!({
    "abc":"abc",
    "efg":"efg"
 });

 let body: Value = reqwest::Client::new()
 .post("http://localhost:4000/hello")
 .json(&map1)
 .send()
 .await?
 .json()
 .await?;

 println!("Data is : {:?}", body);
 let resp = serde_json::to_string(&body).unwrap();
    Ok(resp)

 }
ouflak
  • 2,458
  • 10
  • 44
  • 49
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 28 '21 at 02:39