I’m about to create http client for query some data from server which I had done on python or either test with Httpie it work and server respond as expected but when I use Rust reqwest is always get 411 error (I have try other crates like ureq, hyper, awc also have same issue).
this could be a very simple request script but I dont know why only Rust that return 411 error
this python code
import requests
url = "http://127.0.0.1:3000/GetPut"
querystring = {"TYPE":"GET"}
payload = ":HEADERnTABLE=VERSIONnVERSIONnSTRINGn1.00nTABLE=CURRENT_ATTRIBUTESnSERIAL_NUMnSTRINGnAB22D002000CD3C28n"
headers = {
"User-Agent": "request",
"Host": "127.0.0.1",
"Content-Type": "text/plain; charset=utf-8"
}
response = requests.request("POST", url, data=payload, headers=headers, params=querystring)
print(response.text)
and here Rust with reqwest
let serial_num = "AB22D002000CD3C28";
let body = format!(":HEADERnTABLE=VERSIONnVERSIONnSTRINGn1.00nTABLE=CURRENT_ATTRIBUTESnSERIAL_NUMnSTRINGn{}n",serial_num);
let body_length = body.chars().count().to_string();
let length = body_length.as_str();
let mut headers = HeaderMap::new();
headers.insert(CONTENT_LENGTH, HeaderValue::from_str(length).unwrap());
headers.insert(CONTENT_TYPE, HeaderValue::from_static("text/plain; charset=utf-8"));
headers.insert(HOST, HeaderValue::from_static("127.0.0.1"));
headers.insert(USER_AGENT, HeaderValue::from_static("reqwest"));
let url = "http://127.0.0.1:3000/GetPut";
let query = [("TYPE", "GET")];
let client = reqwest::Client::new();
let (cli, req) = client
.post(url)
.headers(headers)
.query(&query)
.body(body)
.build_split();
let req = req.unwrap();
println!("{:?}",cli);
println!("{:?}",req);
let response = cli.execute(req).await.unwrap();
println!("{:?}", response.headers());
println!("{:?}", response.status());
println!("{}", response.text().await.unwrap());
I have try to check with Python , Httpie with same format and server response as normal only Rust that always get 411 error, I also try ureq, hyper, awc it also same error
Thodsaphorn Kaminthong is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.