【发布时间】:2022-07-05 21:28:12
【问题描述】:
我正在尝试将文件(我打开并读入缓冲区)转换为有效的 BSON 格式。
我编写客户端以发出需要两个字段的请求;
- 文件名
- 文件(缓冲区)
这里的问题是我似乎无法在这里成功转换。
另一个问题是,在进行此转换后,是否可以将此 BSON 请求转换为缓冲区,因为这是 curl(Easy) crate 用于发出请求的类型(即来自终端而不是浏览器的请求表格)
这是我发出此请求的代码
// It takes in a file path.
fn send_file_post(file_from_arg: &str) -> tide::Result {
// initialise the connection to the server
let mut easy = Easy::new();
easy.url("http://0.0.0.0:8080/hi").unwrap();
// Opens and reads the file path
let mut file = File::open(file_from_arg)?;
let mut buf = [0; 1096];
// reads file into buffer
loop {
let n = file.read(&mut buf)?;
if n == 0 {
// reached end of file
break;
}
// easy.write_all(&buf[..n])?;
}
// attempted bson format
let bson_data: Bson = bson!({
"name": file_from_arg,
"file": buf
});
// sending the request, this is only sending the file for now, I want to send a bson format that is buffered (in a buffer/bytes format)
easy.post_fields_copy(&buf).unwrap();
easy.write_function(|data| {
stdout().write_all(data).unwrap();
Ok(data.len())
})
.unwrap();
println!(" oh hi{:?}", easy.perform().unwrap());
Ok(format!("okay sent!").into())
}
【问题讨论】: