simple-server icon indicating copy to clipboard operation
simple-server copied to clipboard

Request body is empty while making post request from "reqwest"

Open jawwadturabi opened this issue 4 years ago • 1 comments

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 the 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.

jawwadturabi avatar Apr 20 '20 11:04 jawwadturabi

same issue

jamil4321 avatar Apr 20 '20 14:04 jamil4321