I'm trying to implement this curl
call using the Rust crate reqwest:
curl -u [USERNAME]:[PASSWORD] -H "Content-Type: application/json" -d @content.json [WEBSITE]
The file content.json
encodes a JSON object as the name suggests.
My code looks like this:
fn get_response() -> Result<String, Box<dyn Error>> {
let content = std::fs::read_to_string("content.json").unwrap();
let client = reqwest::blocking::Client::new();
client
.post([WEBSITE])
.basic_auth([USERNAME], Some([PASSWORD]))
.json(&content)
.send()?
.text()?
While the curl
command works, I get a "malformed request payload" error message in the response when running the Rust code. Unfortunately, I don't have control over the website, so I can't debug it there.
My question is: Am I doing something obviously wrong? If there's no obvious problem, what are some options (e.g., additional headers) that I should try out? (Of course, I already tried a few things but nothing worked)
.body()
and set the content type header toapplication/json
and it still did not work. So, providing a serde serializable type is the only solution that worked for me. – Volitive