[英]How to call direct pdf download api with auth token in react-native using axios
I am callind a api which is directly download pdf and perfectly work in postman but not work in react-native application with axios.我正在调用一个 api,它可以直接下载 pdf 并在邮递员中完美工作,但不能在带有 axios 的 react-native 应用程序中工作。
I also tried to call with blob format but i am getting error which is Cannot create URL for blob!我也尝试使用 blob 格式进行调用,但出现错误,即无法为 blob 创建 URL!
axios({
method: GET,
url: url,
responseType: 'blob',
headers: {
'Content-type': 'application/pdf',
"x-requested-with":"XMLHttpRequest",
Accept: "application/pdf",
Authorization: `Bearer ${token}`,
},
})
.then(response => {
const file = new Blob(
[response.data],
{type: 'application/pdf'});
const fileURL = URL.createObjectURL(file);
window.open(fileURL);
})
.catch(err => {
isDev && console.log('error we got back', err);
});
and without blob format i am getting this bellow values in response:如果没有 blob 格式,我将收到以下响应值:
"03123 00000 n
0000015836 00000 n
0000024222 00000 n
0000000289 00000 n
0000002463 00000 n
0000002483 00000 n
0000002529 00000 n
0000002581 00000 n
0000002633 00000 n
0000002956 00000 n
0000002820 00000 n
0000002697 00000 n
0000003019 00000 n
0000003468 00000 n
0000006174 00000 n
0000003244 00000 n
0000003448 00000 n
0000006195 00000 n
0000006455 00000 n
0000014634 00000 n
0000015056 00000 n
0000014613 00000 n
0000015972 00000 n
0000016237 00000 n
0000023223 00000 n
0000023575 00000 n
0000023202 00000 n
trailer
<<
/Size 32
/Info 1 0 R
/Root 17 0 R
>>
startxref
24461
%%EOF
" ”
const {config, fs} = RNFetchBlob;
let DownloadDir = fs.dirs.DownloadDir;
let options = {
fileCache: true,
addAndroidDownloads: {
useDownloadManager: true,
notification: true,
title: `${name} file report`,
path: DownloadDir + `/${name}Report` + '.pdf',
description: 'Downloading file.',
},
};
config(options)
.fetch(
"GET",
url,
{
Authorization: `Bearer ${access_token}`,
},
)
.then(response => {
console.log('The file saved to ', response .path());
})
.catch(err => {
console.log(' response in catch123', err);
return reject(err);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.