Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jawwadturabi opened this issue Apr 20, 2020 · 1 comment
Open

Comments

@jawwadturabi
Copy link

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.

@jamil4321
Copy link

jamil4321 commented Apr 20, 2020

same issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants