I've been trying to do this multiple ways and it doesn't seem to work. I'm using NextJS so first I call my Next API:
我试了很多种方法,但似乎都不管用。我使用的是NextJS,所以首先我调用我的下一个API:
...
mutateUser(
await fetchJson("/path/to/api", {
method: "POST",
headers: { "Content-Type": "application/pdf" },
body: file, // const [file, setFile] = useState<File | null>(null);
})
);
...
Then I call my Actix Web server using Axios in the API
然后在API中使用Axios调用我的Actix Web服务器
...
const { token } = req.session.user;
const body = req.body;
const api_url = `${process.env.SERVER_API_URL}/path/to/api` as string;
console.log(body.length);
try {
const response = await axios.post(api_url, body, {
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "application/pdf",
},
});
res.json({ ...response.data, isLoggedIn: true });
}
...
Then this is how I handle it in Actix:
下面是我在Actix中处理它的方式:
pub async fn route_function(db: Data<DatabaseRepository>, mut payload: Payload, auth: AuthorizationService) -> HttpResponse {
let id = auth.id;
let mut bytes = BytesMut::new();
while let Some(item) = payload.next().await {
bytes.extend_from_slice(&item.unwrap());
}
//bytes to string
log::debug!("bytes: {:?}", bytes);
// length of bytes
log::debug!("bytes length: {:?}", bytes.len());
let pdf_text = match pdf_extract::extract_text_from_mem(&bytes) {
Ok(text) => text,
Err(e) => { // PDF parsing error only with Axios
log::error!("Error: {:#?}", e);
return HttpResponse::BadRequest().json(ErrorResponse::new("error parsing pdf".to_string(), e.to_string()));
}
};
...
So basically, what's going wrong is this: When I use Postman or cURL the Rust route works perfectly. Here is my cURL command for reference:
所以基本上,问题是这样的:当我使用邮递员或卷曲时,铁锈路线工作得很好。下面是我的curl命令以供参考:
# This works
curl -X POST -H "Content-Type: application/pdf" -H "Authorization: Bearer <TOKEN>" --data-binary "@/path/to/file.pdf" http://localhost:8080/path/to/api
However, when I use Axios, I get a PDF Parsing error. The PDF parser library returns an error when parsing the PDF Trailer. Additionally, I was checking the bytes length of the requests and when I call using Axios it is around 101k bytes but when I call using Postman or cURL it is 58k bytes. Not sure why but I think this has something to do with why it's not working. This also makes me think that I am doing something wrong with either Axios, Next, or Fetch but I can't find where. I also tried using FormData()
but that didn't work either.
然而,当我使用Axios时,我得到一个PDF解析错误。PDF解析器库在解析PDF尾部时返回错误。此外,我检查了请求的字节长度,当我使用Axios调用时,它大约是101K字节,但当我使用Postman或CURL调用时,它是58K字节。不知道为什么,但我认为这与它不起作用有关。这也让我认为我用Axios、Next或Fetch做错了什么,但我找不到在哪里。我也尝试使用FormData(),但也不起作用。
If someone has a workaround by changing my Actix route so that it works generally, this is also appreciated. I only found how to read a pdf using the Payload
extractor.
如果有人通过更改我的Actix路线来解决问题,使其正常工作,这也是非常感谢的。我只知道如何使用有效负载解压程序来阅读pdf。
Any help is appreciated. Thanks in advance.
如有任何帮助,我们不胜感激。先谢谢你。
更多回答
I think Axios tries to do various bits of magic with the request, setting Content-Type
headers and transforming the data; it may be possible to get this working with Axios (I'm not the right person to ask) but it might also be that it's not the right tool for this job.
我认为Axios试图对请求执行各种魔术,设置内容类型标头并转换数据;也许可以使用Axios(我不是合适的人来问),但也可能它不是适合这项工作的工具。
我是一名优秀的程序员,十分优秀!